-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
234 lines (186 loc) · 10.3 KB
/
Copy pathgui.py
File metadata and controls
234 lines (186 loc) · 10.3 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import tkinter as tk
from tkinter import ttk, messagebox
from version import __version__, __changelog__
import psutil
class ConfigWindow:
def __init__(self, config_manager, on_save=None, device_info=None):
self.config_manager = config_manager
self.on_save = on_save
self.device_info = device_info or {}
self.window = None
self.network_combo = None
self._pending_interfaces = None
self.disk_vars = {}
self.disks = []
def _get_disks(self):
disks = []
for partition in psutil.disk_partitions():
try:
usage = psutil.disk_usage(partition.mountpoint)
disks.append({
"device": partition.device,
"mountpoint": partition.mountpoint,
"used": round(usage.used / (1024**3), 2),
"total": round(usage.total / (1024**3), 2),
"percent": usage.percent
})
except:
pass
return disks
def show(self):
if self.window and self.window.winfo_exists():
self.window.lift()
return
self.disks = self._get_disks()
window_height = 590 + len(self.disks) * 30
self.window = tk.Toplevel()
self.window.title(f"PCWatcher 配置 v{__version__}")
self.window.geometry(f"550x{window_height}")
self.window.resizable(False, False)
self._create_widgets()
self._load_config()
if self._pending_interfaces:
self.set_network_interfaces(self._pending_interfaces)
def _create_widgets(self):
main_frame = ttk.Frame(self.window, padding="10")
main_frame.pack(fill=tk.BOTH, expand=True)
main_frame.columnconfigure(1, weight=1)
row = 0
ttk.Label(main_frame, text="设备备注名:").grid(row=row, column=0, sticky=tk.W, pady=5)
self.device_name_var = tk.StringVar()
ttk.Entry(main_frame, textvariable=self.device_name_var, width=38).grid(row=row, column=1, pady=5, sticky=tk.EW)
row += 1
ttk.Label(main_frame, text="设备型号:").grid(row=row, column=0, sticky=tk.W, pady=5)
device_model_text = self.device_info.get("device_model", "")
if not device_model_text:
device_model_text = self.device_info.get("hostname", "未知设备")
ttk.Label(main_frame, text=device_model_text, foreground='gray', anchor='w').grid(row=row, column=1, sticky=tk.EW, pady=5)
row += 1
ttk.Label(main_frame, text="PushMe Key:").grid(row=row, column=0, sticky=tk.W, pady=5)
self.push_key_var = tk.StringVar()
ttk.Entry(main_frame, textvariable=self.push_key_var, width=38).grid(row=row, column=1, pady=5, sticky=tk.EW)
ttk.Button(main_frame, text="测试", command=self._test_pushme, width=6).grid(row=row, column=2, padx=(5, 0))
row += 1
ttk.Label(main_frame, text="CPU 使用率阈值 (%):").grid(row=row, column=0, sticky=tk.W, pady=5)
self.cpu_threshold_var = tk.IntVar(value=80)
ttk.Entry(main_frame, textvariable=self.cpu_threshold_var, width=15).grid(row=row, column=1, sticky=tk.W, pady=5)
row += 1
ttk.Label(main_frame, text="内存使用率阈值 (%):").grid(row=row, column=0, sticky=tk.W, pady=5)
self.memory_threshold_var = tk.IntVar(value=85)
ttk.Entry(main_frame, textvariable=self.memory_threshold_var, width=15).grid(row=row, column=1, sticky=tk.W, pady=5)
row += 1
ttk.Separator(main_frame, orient='horizontal').grid(row=row, column=0, columnspan=3, sticky='ew', pady=10)
row += 1
ttk.Label(main_frame, text="磁盘阈值设置", font=('Microsoft YaHei', 10, 'bold')).grid(row=row, column=0, columnspan=3, sticky=tk.W, pady=5)
row += 1
for disk in self.disks:
mountpoint = disk["mountpoint"]
label = ttk.Label(main_frame, text=f"磁盘 {mountpoint} (%):")
label.grid(row=row, column=0, sticky=tk.W, pady=3)
var = tk.IntVar(value=90)
self.disk_vars[mountpoint] = var
ttk.Entry(main_frame, textvariable=var, width=15).grid(row=row, column=1, sticky=tk.W, pady=3)
row += 1
if not self.disks:
ttk.Label(main_frame, text="未检测到磁盘", foreground='gray').grid(row=row, column=0, columnspan=3, sticky=tk.W, pady=5)
row += 1
row += 1
ttk.Separator(main_frame, orient='horizontal').grid(row=row, column=0, columnspan=3, sticky='ew', pady=10)
row += 1
ttk.Label(main_frame, text="网卡选择:").grid(row=row, column=0, sticky=tk.W, pady=5)
self.network_interface_var = tk.StringVar()
self.network_combo = ttk.Combobox(main_frame, textvariable=self.network_interface_var, width=37)
self.network_combo.grid(row=row, column=1, pady=5)
row += 1
ttk.Label(main_frame, text="上传速度阈值 (MB/s):").grid(row=row, column=0, sticky=tk.W, pady=5)
self.upload_threshold_var = tk.IntVar(value=10)
ttk.Entry(main_frame, textvariable=self.upload_threshold_var, width=15).grid(row=row, column=1, sticky=tk.W, pady=5)
row += 1
ttk.Label(main_frame, text="下载速度阈值 (MB/s):").grid(row=row, column=0, sticky=tk.W, pady=5)
self.download_threshold_var = tk.IntVar(value=10)
ttk.Entry(main_frame, textvariable=self.download_threshold_var, width=15).grid(row=row, column=1, sticky=tk.W, pady=5)
row += 1
ttk.Separator(main_frame, orient='horizontal').grid(row=row, column=0, columnspan=3, sticky='ew', pady=10)
row += 1
ttk.Label(main_frame, text="监控间隔 (秒):").grid(row=row, column=0, sticky=tk.W, pady=5)
self.interval_var = tk.IntVar(value=30)
ttk.Entry(main_frame, textvariable=self.interval_var, width=15).grid(row=row, column=1, sticky=tk.W, pady=5)
row += 1
btn_frame = ttk.Frame(main_frame)
btn_frame.grid(row=row, column=0, columnspan=3, pady=20)
ttk.Button(btn_frame, text="保存", command=self._save_config).pack(side=tk.LEFT, padx=5)
ttk.Button(btn_frame, text="取消", command=self.window.destroy).pack(side=tk.LEFT, padx=5)
ttk.Button(btn_frame, text="关于", command=self._show_about).pack(side=tk.LEFT, padx=5)
def _show_about(self):
about_window = tk.Toplevel(self.window)
about_window.title(f"关于 PCWatcher")
about_window.geometry("450x400")
about_window.resizable(False, False)
header = tk.Frame(about_window, bg='#1E88E5', height=60)
header.pack(fill=tk.X)
header.pack_propagate(False)
tk.Label(header, text=f"PCWatcher v{__version__}", font=('Microsoft YaHei', 14, 'bold'), bg='#1E88E5', fg='white').pack(pady=15)
text_frame = tk.Frame(about_window)
text_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
scrollbar = tk.Scrollbar(text_frame)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
text = tk.Text(text_frame, wrap=tk.WORD, font=('Microsoft YaHei', 9), yscrollcommand=scrollbar.set)
text.pack(fill=tk.BOTH, expand=True)
scrollbar.config(command=text.yview)
text.insert(tk.END, __changelog__)
text.config(state=tk.DISABLED)
tk.Button(about_window, text="关闭", command=about_window.destroy, padx=20).pack(pady=10)
def _load_config(self):
cfg = self.config_manager.config
self.push_key_var.set(cfg.get("push_key", ""))
self.device_name_var.set(cfg.get("device_name", ""))
self.cpu_threshold_var.set(cfg.get("cpu_threshold", 80))
self.memory_threshold_var.set(cfg.get("memory_threshold", 85))
self.network_interface_var.set(cfg.get("network_interface", ""))
self.upload_threshold_var.set(cfg.get("network_upload_threshold", 10485760) // 1048576)
self.download_threshold_var.set(cfg.get("network_download_threshold", 10485760) // 1048576)
self.interval_var.set(cfg.get("interval", 30))
disk_thresholds = cfg.get("disk_thresholds", {})
for mountpoint, var in self.disk_vars.items():
var.set(disk_thresholds.get(mountpoint, 90))
def _test_pushme(self):
from notifier import PushMeClient
key = self.push_key_var.get()
if not key:
messagebox.showwarning("警告", "请输入 PushMe Key")
return
device_name = self.device_name_var.get()
test_device_info = self.device_info.copy()
test_device_info["device_name"] = device_name
client = PushMeClient(key)
success, msg = client.test_connection(test_device_info)
if success:
messagebox.showinfo("成功", "连接测试成功!")
else:
messagebox.showerror("失败", f"连接失败: {msg}")
def _save_config(self):
cfg = self.config_manager.config
cfg["push_key"] = self.push_key_var.get()
cfg["device_name"] = self.device_name_var.get()
cfg["cpu_threshold"] = self.cpu_threshold_var.get()
cfg["memory_threshold"] = self.memory_threshold_var.get()
disk_thresholds = {}
for mountpoint, var in self.disk_vars.items():
disk_thresholds[mountpoint] = var.get()
cfg["disk_thresholds"] = disk_thresholds
cfg["network_interface"] = self.network_interface_var.get()
cfg["network_upload_threshold"] = self.upload_threshold_var.get() * 1048576
cfg["network_download_threshold"] = self.download_threshold_var.get() * 1048576
cfg["interval"] = self.interval_var.get()
cfg["first_run"] = False
self.config_manager.save()
if self.on_save:
self.on_save()
messagebox.showinfo("成功", "配置已保存")
self.window.destroy()
def set_network_interfaces(self, interfaces):
self._pending_interfaces = interfaces
if self.network_combo:
self.network_combo['values'] = interfaces
if interfaces:
self.network_combo.current(0)