• Pyqt 时时CPU使用情况


    借鉴代码来自:https://github.com/hgoldfish/quickpanel

    实现代码:

      1 # -*- coding:utf-8 -*-
      2 from __future__ import print_function
      3 from __future__ import unicode_literals
      4 from __future__ import division
      5 from __future__ import absolute_import
      6 
      7 import ctypes, sys
      8 import ctypes.wintypes
      9 from PyQt4.QtCore import QPoint, QRect, QTimer, Qt
     10 from PyQt4.QtGui import QPainter, QPen, QPolygon, QApplication, QWidget
     11 
     12 GetSystemTimes = ctypes.windll.kernel32.GetSystemTimes
     13 class FILETIME(ctypes.Structure):
     14     _fields_ = [("dwLowDateTime", ctypes.wintypes.DWORD), ("dwHighDateTime", ctypes.wintypes.DWORD)]
     15 
     16     def __int__(self):
     17         # print(self.dwHighDateTime)
     18         return self.dwHighDateTime * 0x100000000 + self.dwLowDateTime
     19 class MachineLoad:
     20     _instance = None
     21 
     22     @staticmethod
     23     def getInstance():
     24         if MachineLoad._instance is None:
     25             MachineLoad._instance = MachineLoad()
     26         return MachineLoad._instance
     27 
     28     def __init__(self):
     29         idle, kernel, user = FILETIME(), FILETIME(), FILETIME()
     30         GetSystemTimes(ctypes.byref(idle), ctypes.byref(kernel), ctypes.byref(user))
     31         self.idle0, self.kernel0, self.user0 = int(idle), int(kernel), int(user)
     32 
     33     def getLoad(self):
     34         idle, kernel, user = FILETIME(), FILETIME(), FILETIME()
     35         GetSystemTimes(ctypes.byref(idle), ctypes.byref(kernel), ctypes.byref(user))
     36         idle1, kernel1, user1 = int(idle), int(kernel), int(user)
     37         a, b, c = idle1 - self.idle0, kernel1 - self.kernel0, user1 - self.user0
     38         self.idle0, self.kernel0, self.user0 = idle1, kernel1, user1
     39         if (b + c) == 0:
     40             return 1
     41         return (b + c - a) / (b + c)
     42 
     43 class MachineLoadWidget(QWidget):
     44     def __init__(self, parent):
     45         QWidget.__init__(self, parent)
     46         self.timer = QTimer()
     47         self.timer.timeout.connect(self.collectMachineLoad)
     48         self.loads = []
     49         self.maxLength = 400
     50         self.pointDistance = 5 #每点之间的间隔
     51         self.updateInterval = 500 #更新的时间间隔
     52         self.timer.setInterval(self.updateInterval)
     53         self.timer.start()
     54         self.machineLoad = MachineLoad.getInstance()
     55         self.boxWidth = 60
     56 
     57     def collectMachineLoad(self):
     58         rate = self.machineLoad.getLoad()
     59         self.loads.insert(0, rate)
     60         if len(self.loads) > self.maxLength:
     61             self.loads.pop(- 1)
     62         if self.isVisible():
     63             self.update()
     64 
     65     def paintEvent(self, event):
     66         QWidget.paintEvent(self, event)
     67         width, height = self.width(), self.height()
     68         polygon = QPolygon()
     69         for i, rate in enumerate(self.loads):
     70             x = width - i * self.pointDistance
     71             y = height - rate * height
     72             if x < self.boxWidth:
     73                 break
     74             polygon.append(QPoint(x, y))
     75         painter = QPainter(self)
     76         pen = QPen()
     77         pen.setColor(Qt.darkGreen)
     78         painter.setPen(pen)
     79         painter.setRenderHint(QPainter.Antialiasing, True)
     80         #画网格
     81         painter.setOpacity(0.5)
     82         gridSize = self.pointDistance * 4
     83         deltaX = (width - self.boxWidth) % gridSize + self.boxWidth
     84         deltaY = height % gridSize
     85         for i in range(int(width / gridSize)):
     86             x = deltaX + gridSize * i
     87             painter.drawLine(x, 0, x, height)
     88         for j in range(int(height / gridSize)):
     89             y = j * gridSize + deltaY
     90             painter.drawLine(self.boxWidth, y, width, y)
     91         #画折线
     92         pen.setColor(Qt.darkCyan)
     93         pen.setWidth(2)
     94         painter.setPen(pen)
     95         painter.setOpacity(1)
     96         painter.drawPolyline(polygon)
     97         #画展示框
     98         if len(self.loads) > 0:
     99             rate = self.loads[0]
    100         else:
    101             rate = 1.0
    102         rect1 = QRect(4, height * 0.05, self.boxWidth - 9, height * 0.7)
    103         rect2 = QRect(4, height * 0.8, self.boxWidth - 9, height * 0.2)
    104         centerX = int(rect1.width() / 2) + 1
    105         pen.setWidth(1)
    106         for i in range(rect1.height()):
    107             if i % 4 == 0:
    108                 continue
    109             if (rect1.height() - i) / rect1.height() > rate:
    110                 pen.setColor(Qt.darkGreen)
    111             else:
    112                 pen.setColor(Qt.green)
    113             painter.setPen(pen)
    114             for j in range(rect1.width()):
    115                 if centerX - 1 <= j <= centerX + 1:
    116                     continue
    117                 painter.drawPoint(rect1.x() + j, rect1.y() + i)
    118         pen.setColor(Qt.black)
    119         painter.setPen(pen)
    120         painter.drawText(rect2, Qt.AlignHCenter | Qt.AlignVCenter, str(int(rate * 100)) + "%")
    121 
    122 
    123 
    124 class CPUstatus(QWidget):
    125     def __init__(self):
    126         super(CPUstatus, self).__init__()
    127         self.resize(200,200)
    128         self.factory = MachineLoadWidget(self)
    129         self.factory.resize(200, 200)
    130 
    131 
    132 if __name__ == "__main__":
    133     app = QApplication(sys.argv)
    134     platform = CPUstatus()
    135     platform.show()
    136     sys.exit(app.exec_())

    效果:

        

  • 相关阅读:
    javascript 取整
    jQueryDom——Select、Option
    数据反解析
    获取MAC地址最有效方法作为软件加密时用到在不同系统平台下
    PowerDesigner的逆向工程.
    操作IC卡
    刷身份证读出相关信息
    控制弹出窗口样式
    软件项目或者产品有时候会用到本机网卡的物理MAC地址、IP地址、硬盘序列号加上使用软体名称和地址进行安全加密验证,例如结合MAC地址进行登录验证等等,当然这种项目一般在局域网内运行,能相对提高系统的安全性
    主要驗證是否有五個星期五五個星期六五個星期天
  • 原文地址:https://www.cnblogs.com/dcb3688/p/4440981.html
Copyright © 2020-2023  润新知