runcat-pyqt5-win:在windows任务栏养猫
V1
runcat-pyqt5-win
RunCat是一款 mac应用,用奔跑的猫来显示当前系统资源(CPU)占用情况。
但是只有mac版,于是用python撸了一个,可以在windows任务栏(通知区域)养猫。
首先用psutil
获得CPU或内存的使用情况,然后用pyqt5创建QSystemTrayIcon显示在任务栏的托盘区域。
GPU的使用情况可以用 nvidia-ml-py, https://pypi.org/project/nvidia-ml-py/
的pynvml模块(仅限nvidia gpu)。
运行效果
Requirements
- psutil
- pyqt5
- nvidia-ml-py
Code
CPU 版
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37import sys
import time
import threading
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon
import psutil
# Get cpu usage
def func():
while True:
global cpu
cpu = psutil.cpu_percent(interval=1) / 100
time.sleep(1)
# Create Qt App
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
# Create trayicon
tray = QSystemTrayIcon()
tray.setIcon(QIcon('0.ico'))
tray.setVisible(True)
cpu = 0.1
timer = threading.Timer(1, func, [])
timer.start()
while True:
t = (cpu * cpu - 10 * cpu + 10) / 40
for i in range(5):
# Update trayicon
tray.setIcon(QIcon('{}.ico'.format(i)))
tray.setToolTip('CPU: {:.2%}'.format(cpu))
time.sleep(t)
app.exec_()内存版
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37import sys
import time
import threading
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon
import psutil
# Get memory usage
def func():
while True:
global mem
mem = psutil.virtual_memory().percent / 100
time.sleep(1)
# Create Qt App
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
# Create trayicon
tray = QSystemTrayIcon()
tray.setIcon(QIcon('0.ico'))
tray.setVisible(True)
mem = 0.1
timer = threading.Timer(1, func, [])
timer.start()
while True:
t = (mem * mem - 10 * mem + 10) / 40
for i in range(5):
# Update trayicon
tray.setIcon(QIcon('{}.ico'.format(i)))
tray.setToolTip('Memory: {:.2%}'.format(mem))
time.sleep(t)
app.exec_()GPU 版
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41import sys
import time
import threading
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon
import pynvml
pynvml.nvmlInit()
handle = pynvml.nvmlDeviceGetHandleByIndex(0) # GPU id: 0
# Get gpu usage
def func():
while True:
global gpu
meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
gpu = meminfo.used / meminfo.total
time.sleep(1)
# Create Qt App
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
# Create trayicon
tray = QSystemTrayIcon()
tray.setIcon(QIcon('0.ico'))
tray.setVisible(True)
gpu = 0.1
timer = threading.Timer(1, func, [])
timer.start()
while True:
t = (gpu * gpu - 10 * gpu + 10) / 40
for i in range(5):
# Update trayicon
tray.setIcon(QIcon('{}.ico'.format(i)))
tray.setToolTip('GPU: {:.2%}'.format(gpu))
time.sleep(t)
app.exec_()
Usage
直接clone或下载, 改 *.pyw 运行
或者下载打包后的 *.exe, 但是有30多M。。。下载exe
ref:
V2
之前的功能比较简单,这次增加了右键菜单:
- 可切换图标类型: [cat, mario]
- 可切换监控类型: [cpu, memory, gpu(nVidia)]
- 增加了退出按钮
运行效果
Requirements
- psutil
- pyqt5
- nvidia-ml-py
完整代码
1 | import os |
V3
原来的基于 ptqt5 库比较大; 这次采用 pystray 轻量实现:
Requirements
- psutil
- pystray
完整代码
1 | import threading |