借鉴代码来自: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. class CPUstatus(QWidget):
  123. def __init__(self):
  124. super(CPUstatus, self).__init__()
  125. self.resize(200,200)
  126. self.factory = MachineLoadWidget(self)
  127. self.factory.resize(200, 200)
  128.  
  129. if __name__ == "__main__":
  130. app = QApplication(sys.argv)
  131. platform = CPUstatus()
  132. platform.show()
  133. sys.exit(app.exec_())

效果:

    

Pyqt 时时CPU使用情况的更多相关文章

  1. 全面了解 Linux 服务器 - 1. 查看 Linux 服务器的 CPU 详细情况

    1. 查看 Linux 服务器的 CPU 详细情况 判断依据: 具有相同的 core id 的 CPU 是同意个 core 超线程. 具有相同的 physical id 的 CPU 是同一个 CPU ...

  2. linux中如何查看进程对应的cpu使用情况?

    使用ps aux | grep <进程名>即可查看指定进程的cpu使用情况.

  3. ubuntu查看内存占用和查看cpu使用情况的简单方法(ubuntu内存管理)

    单独查看内存使用情况的命令:free -m查看内存及cpu使用情况的命令:top也可以安装htop工具,这样更直观,安装命令如下:sudo apt-get install htop安装完后,直接输入命 ...

  4. 获取CPU使用情况信息(转)

    获取了内存使用情况,也可以使用PHP的 getrusage()获取CPU使用情况,该方法在windows下不可用.    print_r(getrusage()); /* 输出 Array ( [ru ...

  5. 根据dba_hist_osstat统计CPU占用情况

    在11g里面,视图dba_hist_osstat用来记录OS级别的time时间指标.视图dba_hist_osstat_name显示了相关的指标名称. SYS@/dzgddb> select * ...

  6. centos文件/文件夹操作-检查磁盘、内存、cpu使用情况-vi操作命令

    Part1:CentOS文件/文件夹操作 1.新建文件夹 即创建目录 mkdir 文件名 新建一个名为test的文件夹在home下 vi source1 mkdir /home/test 注意:当创建 ...

  7. adb命令检测apk启动时间、内存、CPU使用情况、流量、电池电量等——常用的adb命令

    ADB:Android Debug Bridge,是Android SDK里一个可以直接操作安卓模拟器或真实设备的工具,颇为强大.   检测APP:   adb shell am start -W p ...

  8. Linux评估 CPU使用情况

    评价参数 1)CPU utilization:最直观最重要的就是CPU的使用率.如果长期超过80%,则表明CPU遇到了瓶颈:2)User time: 用户进程使用的CPU:该数值越高越好,表明越多的C ...

  9. CPU使用情况检测

    改编自:https://blog.csdn.net/Yan_Chou/article/details/80456995 检测命令整理: dd iotop df top psiostatvmstatne ...

随机推荐

  1. note:获取字符输入的一些函数

    总是弄混,所以总结一下: getline()     // 接受一个字符串,可以接收空格并输出,需包含“#include<string>” #include<iostream> ...

  2. putty如何使用

    使用putty连接管理centos 1 双击putty.exe2 和linux命令行一样了 使用psftp上传和下载 cd d:/psftppsftp open 10.0.0.9 输入用户密码root ...

  3. java78_c

    import java.util.*; public class Main { public static void main(String args[]){ Scanner cin=new Scan ...

  4. jquery 生成table表格 部分代码

    想生成上面这样的table表格先看返回数据格式 <div id="create_img_tab"></div> window.onload = functi ...

  5. 7.6--找过点最多的直线(CC150)

    直接两个点确定一条直线.然后两两组合,再写一个看过多少个点的函数.一直更新max就行. import java.util.Arrays; public class Solution { public ...

  6. [转]Python的ASCII, GB2312, Unicode , UTF-8

    2007-12-13 10:50:47|  分类: Python实用软件编|举报|字号 订阅     ASCII 是一种字符集,包括大小写的英文字母.数字.控制字符等,它用一个字节表示,范围是 0-1 ...

  7. 巧用linux服务器的/dev/shm/,如果合理使用,可以避开磁盘IO不给力,提高网站访问速度。

    巧用linux服务器的/dev/shm/ 巧用linux服务器的/dev/shm/,如果合理使用,可以避开磁盘IO不给力,提高网站访问速度. 首先让我们认识一下,什么是tmpfs和/dev/shm/? ...

  8. jquery实现input输入框实时输入触发事件代码 ---jQuery 中bind(),live(),delegate(),on() 区别

    复制代码 代码如下: <input id="productName" name="productName" value="" /> ...

  9. SQL Server常用命令

    1.DECLARE DECLARE命令用于声明一个或多个局部变量.游标变量或表变量. 注:如果定义的变量是字符型,应该指定data_type表达式中其最大长度,否则系统认为其长度为1. declare ...

  10. 设计算法,求AB两个整数集合的交集

    [本文链接] http://www.cnblogs.com/hellogiser/p/ab-set-intersection.html [分析] 思路1:排序法 对集合A和集合B进行排序(升序,用快排 ...