-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResourceMonitorPublic.py
63 lines (48 loc) · 1.96 KB
/
ResourceMonitorPublic.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import tkinter as tk
from tkinter import ttk
import psutil
from win10toast import ToastNotifier
import time
class SystemMonitor(tk.Tk):
def __init__(self):
self.cpu_cooldown = 0
self.memory_cooldown = 0
super().__init__()
# 无边框,置顶
self.overrideredirect(True)
self.wm_attributes("-topmost", True)
# 黑色背景
self.config(bg='black')
# 创建界面元素
self.cpu_var = tk.StringVar()
self.cpu_label = ttk.Label(self, textvariable=self.cpu_var, foreground='white', background='black')
self.cpu_label.pack()
self.memory_var = tk.StringVar()
self.memory_label = ttk.Label(self, textvariable=self.memory_var, foreground='white', background='black')
self.memory_label.pack()
# 刷新数据
self.refresh()
# 绑定按键
self.bind("<Escape>", self.close)
def refresh(self):
# 获取并显示占用
cpu_percent = psutil.cpu_percent()
self.cpu_var.set(f'CPU Usage: {cpu_percent}%')
memory_percent = psutil.virtual_memory().percent
self.memory_var.set(f'Memory Usage: {memory_percent}%')
# 报警
if cpu_percent > 90 and time.time() - self.cpu_cooldown > 60:
toast.show_toast("CPU Warning", f'CPU Usage: {cpu_percent}%', duration=5)
self.cpu_cooldown = time.time()
if memory_percent > 90 and time.time() - self.memory_cooldown > 60:
toast.show_toast("Memory Warning", f'Memory Usage: {memory_percent}%', duration=5)
self.memory_cooldown = time.time()
# 每秒刷新
self.after(1000, self.refresh)
def close(self, event):
exit()
# 退出
if __name__ == "__main__":
toast = ToastNotifier()
monitor = SystemMonitor()
monitor.mainloop()