首页 > 代码库 > pyqt练习x3
pyqt练习x3
# -*- coding: utf-8 -*-
__author__ = ‘Administrator‘
from PyQt4 import QtCore,Qt,QtGui
import sys,time,datetime,os,sched,psutil,wmi
from tools import Ui_Form
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.setFixedSize(825,482)
self._tools.edit__t_edit01.setReadOnly(True)#是否只读
self._tools.label_14.setVisible(0)
self.timers_1=QtCore.QTimer(self)
self.min_1()
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.min_1)
self.timers_1.timeout.connect(self.sys_version)
self.rumsc()
self.timers_1.start(1000)
def sys_version(self):
c=wmi.WMI()
for sys1 in c.Win32_OperatingSystem():
self._tools.threaings_python.setText(unicode(sys1.NumberOfProcesses)+u‘个‘)#当前系统运行的进程总数
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‘]))
def rumsc(self):
self.n=QtCore.QTimer(self)
self.rum()
self.n.timeout.connect(self.rum)
self.n.start(1000)
def rum(self):
today=datetime.datetime.now()
if today.now().strftime(‘%H:%M:%S‘)==‘22:00:00‘:
self._tools.label_14.setVisible(1)
self.n.setInterval(3000)
if today.now().strftime(‘%H:%M:%S‘)!=‘22:00:00‘:
self._tools.label_14.setVisible(0)
def getCPUstate(self,interval=1):
return (" CPU: " + str(psutil.cpu_percent(interval)) + "%")
#cpu_percent(interval=0.1, percpu=False)
# 返回一个浮点数,表示整个系统CPU的利用率,百分比。
def getMemorystate(self):
phymem = psutil.phymem_usage()
#psutil.phymem_usage() 可能和总内存有关
buffers = getattr(psutil, ‘phymem_buffers‘, lambda: 0)()
cached = getattr(psutil, ‘cached_phymem‘, lambda: 0)()
used = phymem.total - (phymem.free + buffers + cached)#phymem.total总内存,free剩余内存
line = u"内存: %5s%% " % ( phymem.percent)
line1=u‘使用当前内存:%6s/%s‘%(
str(int(used / 1024 / 1024)) + "M",
str(int(phymem.total / 1024 / 1024)) + "M")
self._tools.threaings_python_cache.setText(unicode(line))
self._tools.threaings_python_cache.setToolTip(unicode(line1))
def poll(self,interval):
tot_before = psutil.network_io_counters()#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.n.start(1000)
self._tools.threaings_python_cpu.setText(unicode(cpu_state))
def min_1(self):
interval = 1
args = self.poll(interval)
self.refresh_window(*args)
interval = 1
def main():
app=QtGui.QApplication([])
extools=ExTools()
extools.show()
sys.exit(app.exec_())
main()
如图:
安装python模块方法如下:http://blog.163.com/yang_jianli/blog/static/161990006201162152724339/
wmi模块学习:http://www.linuxidc.com/Linux/2013-03/81188.htm
文件:wmi下载地址:http://files.git.oschina.net/group1/M00/00/42/cHwGbFRmzhqAIZXKAABpPpSaZiI889.zip?token=277cc5c1ee23aca4c63a358820e19fe5&ts=1416023594&filename=WMI-1.4.9.zip
pyqt文件:http://files.git.oschina.net/group1/M00/00/42/cHwGbFRmziaAFzoHAARNzxOwsyQ097.rar?token=41867f51dad97acec2c1dae9216d1924&ts=1416023594&filename=pyqt%E7%BB%83%E4%B9%A0x3.rar
pyqt练习x3