标签:
# -*- coding: utf-8 -*-
__author__ = ‘Administrator‘
from PyQt4 import QtCore,Qt,QtGui
import sys,time,datetime,os,wmi,sched,psutil
from PyQt4.QtCore import Qt
from tools import Ui_Form
from cpuckack import Ui_Form1
class ExTools(QtGui.QDialog):
def __init__(self,parent=None):
super(ExTools,self).__init__(parent)
self._tools=Ui_Form()
self._tools.setupUi(self)
self.setWindowIcon(QtGui.QIcon(r‘imags\top.png‘))
self.setWindowTitle(u‘微末工具‘)
self._tools.edit__t_edit01.setReadOnly(True)#是否只读
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)#去掉标题栏
self.timers_1=QtCore.QTimer(self)
#self.scheduc=sched.scheduler(time.time,time.sleep)
self.timers_1.timeout.connect(self.Times1)
self.timers_1.timeout.connect(self.Times2)
self.timers_1.timeout.connect(self.Times3)
self.timers_1.timeout.connect(self.Times4)
self.timers_1.timeout.connect(self.Times5)
self.timers_1.timeout.connect(self.getMemorystate)
self.timers_1.timeout.connect(self.getCPUstate)
self.timers_1.timeout.connect(self.sup)
self.timers_1.start(1000)
self._tools.label_14.setVisible(0)
self._tools.close_button.clicked.connect(self.closeEvent)
self._tools.min_button.clicked.connect(self.minx)
self.createContexMenu()
self._tools.threaings_python_cpu.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.connect(self._tools.threaings_python_cpu,QtCore.SIGNAL(‘customContextMenuRequested(const QPoint&)‘),self.on_context)
self.cpuduo=Ups()
def Times1(self):
today=datetime.datetime.now()
self._tools.lebs.setText(unicode(today.hour))
def Times2(self):
today=datetime.datetime.now()
self._tools.lebs_2.setText(unicode(today.minute))
def Times3(self):
today=datetime.datetime.now()
self._tools.lebs_3.setText(unicode(today.second))
def Times4(self):
today=datetime.datetime.now()
self._tools.lebs_4.setText(unicode(today.year))
def Times5(self):
today=datetime.datetime.now()
dict={‘am‘:u‘早上‘,‘pm‘:u‘晚上‘,‘sw‘:u‘上午‘,‘xw‘:u‘下午‘}
#self._tools.lebs_5.setText(unicode(today.strftime(‘%p‘)))
if today.hour>=0 and today.hour<6:
self._tools.lebs_5.setText(unicode(dict[‘am‘]))
elif today.hour>=6 and today.hour<11:
self._tools.lebs_5.setText(unicode(dict[‘sw‘]))
elif today.hour>=11 and today.hour<18:
self._tools.lebs_5.setText(unicode(dict[‘xw‘]))
else:
self._tools.lebs_5.setText(unicode(dict[‘pm‘]))
if today.strftime(‘%H:%M:%S‘)==‘22:00:00‘:
self._tools.label_14.setVisible(1)
self.timers_1.setInterval(3000)
if today.strftime(‘%H:%M:%S‘)!=‘22:00:00‘:
self._tools.label_14.setVisible(0)
def getCPUstate(self,interval=1):
self._tools.threaings_python_cpu.setText( str(psutil.cpu_percent(interval)) + "%")
def getMemorystate(self):
phymem = psutil.phymem_usage()
buffers = getattr(psutil, ‘phymem_buffers‘, lambda: 0)()
cached = getattr(psutil, ‘cached_phymem‘, lambda: 0)()
used = phymem.total - (phymem.free + buffers + cached)
line = "%5s%% %6s/%s" % (
phymem.percent,
str(int(used / 1024 / 1024)) + "M",
str(int(phymem.total / 1024 / 1024)) + "M")
self._tools.threaings_python_cache.setText(unicode(line))
def poll(self,interval):
tot_before = psutil.network_io_counters()
pnic_before = psutil.network_io_counters(pernic=True)
tot_after = psutil.network_io_counters()
pnic_after = psutil.network_io_counters(pernic=True)
cpu_state = self.getCPUstate(interval)
memory_state = self.getMemorystate()
return (tot_before, tot_after, pnic_before, pnic_after,cpu_state,memory_state)
def refresh_window(self,tot_before, tot_after, pnic_before, pnic_after,cpu_state,memory_state):
self._tools.threaings_python_cpu.setText(str(cpu_state))
def sup(self):
c = wmi.WMI ()
for sys in c.Win32_OperatingSystem():
self._tools.threaings_python.setText(str(sys.NumberOfProcesses)) #当前系统运行的进程总数
def closeEvent(self, e):
close=QtGui.QMessageBox(self)
close.setWindowTitle(u‘关闭提示?‘)
close.setText(u‘是否真的要退出?‘)
n1=close.addButton(u‘退出‘,QtGui.QMessageBox.ActionRole)
n2=close.addButton(u‘取消‘,QtGui.QMessageBox.ActionRole)
n3=close.addButton(u‘关于‘,QtGui.QMessageBox.ActionRole)
close.exec_()
if close.clickedButton()==n1:
sys.exit(0)
elif close.clickedButton()==n2:
pass
else:
QtGui.QMessageBox.information(self,u‘关于‘,u‘谢谢使用‘)
def minx(self):
self.showMinimized()#最小化
def on_context(self,point):
self.contextMenu.exec_(self._tools.threaings_python_cpu.mapToGlobal(point))
def showContextMenu1(self,pos):
self.createContexMenu.move(self.pos()+pos)
self.createContexMenu.show()
def createContexMenu(self):
"""
创建右击菜单
"""
self.setContextMenuPolicy(Qt.CustomContextMenu)
#self.customContextMenuRequested.connect(self.showContextMenu)
#创建QMENU
self.contextMenu=QtGui.QMenu(self)
self.actionA=self.contextMenu.addAction(u‘设置....‘)
self.actionA.triggered.connect(self.sets)
def sets(self):
self.cpuduo.show()
class Ups(QtGui.QDialog):
def __init__(self,parent=None):
super(Ups,self).__init__(parent)
self.cpus=Ui_Form1()
self.cpus.setupUi(self)
self.cpus.q_no.clicked.connect(self.closeEvent)
self.cpus.q_yes.clicked.connect(self.N_cpu)
self.cpus.q_yes_2.clicked.connect(self.N_cachk)
self.cpus.cpu_100.textChanged.connect(self.N_cpu)
def closeEvent(self, enevt):
self.close()
def N_cpu(self,num):
if self.cpus.cpu_100.text()==‘‘:
QtGui.QMessageBox.information(self,u‘非法数据‘,u‘当前cpu使用率不能为空,请输入‘)
else:
return ‘%s‘%self.cpus.cpu_100.setText(str(num))
def N_cachk(self):
self.cpus.chack_100.setText(unicode(QtGui.QMessageBox.information(self,u‘恭喜‘,u‘功能未存在‘)))
def main():
app=QtGui.QApplication([])
extools=ExTools()
extools.show()
sys.exit(app.exec_())
main()
标签:
原文地址:http://www.cnblogs.com/mhxy13867806343/p/4058387.html