Pyqt 时时CPU使用情况
借鉴代码来自: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使用情况的更多相关文章
- 全面了解 Linux 服务器 - 1. 查看 Linux 服务器的 CPU 详细情况
1. 查看 Linux 服务器的 CPU 详细情况 判断依据: 具有相同的 core id 的 CPU 是同意个 core 超线程. 具有相同的 physical id 的 CPU 是同一个 CPU ...
- linux中如何查看进程对应的cpu使用情况?
使用ps aux | grep <进程名>即可查看指定进程的cpu使用情况.
- ubuntu查看内存占用和查看cpu使用情况的简单方法(ubuntu内存管理)
单独查看内存使用情况的命令:free -m查看内存及cpu使用情况的命令:top也可以安装htop工具,这样更直观,安装命令如下:sudo apt-get install htop安装完后,直接输入命 ...
- 获取CPU使用情况信息(转)
获取了内存使用情况,也可以使用PHP的 getrusage()获取CPU使用情况,该方法在windows下不可用. print_r(getrusage()); /* 输出 Array ( [ru ...
- 根据dba_hist_osstat统计CPU占用情况
在11g里面,视图dba_hist_osstat用来记录OS级别的time时间指标.视图dba_hist_osstat_name显示了相关的指标名称. SYS@/dzgddb> select * ...
- centos文件/文件夹操作-检查磁盘、内存、cpu使用情况-vi操作命令
Part1:CentOS文件/文件夹操作 1.新建文件夹 即创建目录 mkdir 文件名 新建一个名为test的文件夹在home下 vi source1 mkdir /home/test 注意:当创建 ...
- adb命令检测apk启动时间、内存、CPU使用情况、流量、电池电量等——常用的adb命令
ADB:Android Debug Bridge,是Android SDK里一个可以直接操作安卓模拟器或真实设备的工具,颇为强大. 检测APP: adb shell am start -W p ...
- Linux评估 CPU使用情况
评价参数 1)CPU utilization:最直观最重要的就是CPU的使用率.如果长期超过80%,则表明CPU遇到了瓶颈:2)User time: 用户进程使用的CPU:该数值越高越好,表明越多的C ...
- CPU使用情况检测
改编自:https://blog.csdn.net/Yan_Chou/article/details/80456995 检测命令整理: dd iotop df top psiostatvmstatne ...
随机推荐
- php curl 实例+详解
直接上实例 <?php //创建一个新cURL资源 $ch = curl_init(); //用于中文等特殊字符的url转码 $aurl = urlencode($address); $url= ...
- am335x UART1输入u-boot 调试信息代码修改
AM335x 调试信息UART1输出代码修改1. 关于pin_mux 的配置代码修改位置:/board/forlinx/ok335x/mux.c void enable_uart0_pin_mux( ...
- .oi 小游戏
http://agar.io/ http://diep.io/ http://slither.io/ http://splix.io/ http://wilds.io/ http://kingz.io ...
- 13 HashTable抽象哈希表类——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...
- Java abstract
abstract修饰符可以修饰类和方法. (1)abstract修饰类,会使这个类成为一个抽象类,这个类将不能生成对象实例,但可以做为对象变量声明的类型,也就是编译时类型.抽象类就相当于一类的半成品, ...
- 10. JEB1.5 插件编写二
一些实例 1. 遍历当前光标处函数所有的Element Java代码: import java.io.*; import java.util.List; import jeb.api.IScript; ...
- 介绍 .Net工具Code Snippet 与 Sql Server2008工具SSMS Tools Pack
不久前,某某在微软写了一个很酷的工具:Visual Stuido2008可视化代码片断工具,这个工具可以在http://www.codeplex.com/SnippetDesigner上免费下载,用它 ...
- close与shutdown函数
linux网络编程之socket(十):shutdown 与 close 函数的区别 http://blog.csdn.net/yijiu0711/article/details/17349169 ...
- 使用Grub Rescue恢复Ubuntu引导
装了Ubuntu和Window双系统的电脑,通常会使用Ubuntu的Grub2进行引导. Grub2会在MBR写入引导记录,并将引导文件放在/boot/grub,破坏任意一项都会导致系统无法正常启动. ...
- 关闭window 8.1 的skydrive
gpedit.msc-->计算机配置-->管理模板-->windows组件 -->skydrive-->阻止使用skydrive执行文件存储