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

实现代码:

 # -*- coding:utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import import ctypes, sys
import ctypes.wintypes
from PyQt4.QtCore import QPoint, QRect, QTimer, Qt
from PyQt4.QtGui import QPainter, QPen, QPolygon, QApplication, QWidget GetSystemTimes = ctypes.windll.kernel32.GetSystemTimes
class FILETIME(ctypes.Structure):
_fields_ = [("dwLowDateTime", ctypes.wintypes.DWORD), ("dwHighDateTime", ctypes.wintypes.DWORD)] def __int__(self):
# print(self.dwHighDateTime)
return self.dwHighDateTime * 0x100000000 + self.dwLowDateTime
class MachineLoad:
_instance = None @staticmethod
def getInstance():
if MachineLoad._instance is None:
MachineLoad._instance = MachineLoad()
return MachineLoad._instance def __init__(self):
idle, kernel, user = FILETIME(), FILETIME(), FILETIME()
GetSystemTimes(ctypes.byref(idle), ctypes.byref(kernel), ctypes.byref(user))
self.idle0, self.kernel0, self.user0 = int(idle), int(kernel), int(user) def getLoad(self):
idle, kernel, user = FILETIME(), FILETIME(), FILETIME()
GetSystemTimes(ctypes.byref(idle), ctypes.byref(kernel), ctypes.byref(user))
idle1, kernel1, user1 = int(idle), int(kernel), int(user)
a, b, c = idle1 - self.idle0, kernel1 - self.kernel0, user1 - self.user0
self.idle0, self.kernel0, self.user0 = idle1, kernel1, user1
if (b + c) == 0:
return 1
return (b + c - a) / (b + c) class MachineLoadWidget(QWidget):
def __init__(self, parent):
QWidget.__init__(self, parent)
self.timer = QTimer()
self.timer.timeout.connect(self.collectMachineLoad)
self.loads = []
self.maxLength = 400
self.pointDistance = 5 #每点之间的间隔
self.updateInterval = 500 #更新的时间间隔
self.timer.setInterval(self.updateInterval)
self.timer.start()
self.machineLoad = MachineLoad.getInstance()
self.boxWidth = 60 def collectMachineLoad(self):
rate = self.machineLoad.getLoad()
self.loads.insert(0, rate)
if len(self.loads) > self.maxLength:
self.loads.pop(- 1)
if self.isVisible():
self.update() def paintEvent(self, event):
QWidget.paintEvent(self, event)
width, height = self.width(), self.height()
polygon = QPolygon()
for i, rate in enumerate(self.loads):
x = width - i * self.pointDistance
y = height - rate * height
if x < self.boxWidth:
break
polygon.append(QPoint(x, y))
painter = QPainter(self)
pen = QPen()
pen.setColor(Qt.darkGreen)
painter.setPen(pen)
painter.setRenderHint(QPainter.Antialiasing, True)
#画网格
painter.setOpacity(0.5)
gridSize = self.pointDistance * 4
deltaX = (width - self.boxWidth) % gridSize + self.boxWidth
deltaY = height % gridSize
for i in range(int(width / gridSize)):
x = deltaX + gridSize * i
painter.drawLine(x, 0, x, height)
for j in range(int(height / gridSize)):
y = j * gridSize + deltaY
painter.drawLine(self.boxWidth, y, width, y)
#画折线
pen.setColor(Qt.darkCyan)
pen.setWidth(2)
painter.setPen(pen)
painter.setOpacity(1)
painter.drawPolyline(polygon)
#画展示框
if len(self.loads) > 0:
rate = self.loads[0]
else:
rate = 1.0
rect1 = QRect(4, height * 0.05, self.boxWidth - 9, height * 0.7)
rect2 = QRect(4, height * 0.8, self.boxWidth - 9, height * 0.2)
centerX = int(rect1.width() / 2) + 1
pen.setWidth(1)
for i in range(rect1.height()):
if i % 4 == 0:
continue
if (rect1.height() - i) / rect1.height() > rate:
pen.setColor(Qt.darkGreen)
else:
pen.setColor(Qt.green)
painter.setPen(pen)
for j in range(rect1.width()):
if centerX - 1 <= j <= centerX + 1:
continue
painter.drawPoint(rect1.x() + j, rect1.y() + i)
pen.setColor(Qt.black)
painter.setPen(pen)
painter.drawText(rect2, Qt.AlignHCenter | Qt.AlignVCenter, str(int(rate * 100)) + "%") class CPUstatus(QWidget):
def __init__(self):
super(CPUstatus, self).__init__()
self.resize(200,200)
self.factory = MachineLoadWidget(self)
self.factory.resize(200, 200) if __name__ == "__main__":
app = QApplication(sys.argv)
platform = CPUstatus()
platform.show()
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. Codeforces 519 E. A and B and Lecture Rooms

    Description 询问一个树上与两点距离相等的点的个数. Sol 倍增求LCA. 一棵树上距离两点相等,要么就只有两点的中点,要么就是与中点相连的所有点. 有些结论很容易证明,如果距离是偶数,那 ...

  2. 关于mysql安全

    修改root用户密码: update mysql.user set password=password('new_passwd') where user='root'; flush privilege ...

  3. django的views里面的request对象详解大全

    简介 HTTP 应用的信息是通过 请求报文 和 响应报文 传递的,关于更多的相关知识,可以阅读<HTTP权威指南>获得. 其中 请求报文 由客户端发送,其中包含和许多的信息,而 djang ...

  4. 常见的MYSQL高可用解决方案

    MySQL 是一种关系数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性.MySQL 软件采用了双授权政策(本词条"授权政策& ...

  5. [转]JVM内存堆布局图解分析

    JAVA能够实现跨平台的一个根本原因,是定义了class文件的格式标准,凡是实现该标准的JVM都能够加载并解释该class文件,据此也可以知道,为啥Java语言的执行速度比C/C++语言执行的速度要慢 ...

  6. 开源工作流 Bonita BPM (JAVA)

    Bonita BPM 开源工作流 Bonita BPM  (JAVA) http://www.bonitasoft.com/

  7. MAC OS X 常用通用快捷键

    注:由于使用的是Windows键盘,习惯了Ctrl + c/v复制粘贴,所以修改了修饰键,Command(⌘)键和Control(^)键互换,以下的Ctrl键均为Command键,对应键盘上的实际左C ...

  8. ASM:《X86汇编语言-从实模式到保护模式》第14章:保护模式下的特权保护和任务概述

    ★PART1:32位保护模式下任务的隔离和特权级保护  这一章是全书的重点之一,这一张必须要理解特权级(包括CPL,RPL和DPL的含义)是什么,调用门的使用,还有LDT和TSS的工作原理(15章着重 ...

  9. uva 1584.Circular Sequence

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  10. Valentine's Day Round 1001.Ferries Wheel(hdu 5174)解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5174 题目意思:给出 n 个人坐的缆车值,假设有 k 个缆车,缆车值 A[i] 需要满足:A[i−1] ...