-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
857 lines (693 loc) · 29.6 KB
/
gui.py
File metadata and controls
857 lines (693 loc) · 29.6 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
"""
Droid Monitor GUI - 中文完整版
特点:
1. 紧凑窗口 1000x700
2. 中文界面,使用微软雅黑字体
3. 恢复所有功能:高级设置
4. 自动刷新余额(替代监控)
5. 移除策略功能
"""
import customtkinter as ctk
from tkinter import messagebox
import threading
from datetime import datetime
from droid_monitor import DroidMonitor, SwitchStrategy
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("blue")
class DroidMonitorGUI:
def __init__(self, root):
self.root = root
self.root.title("Droid Monitor - 余额监控")
# 窗口大小
window_width = 1000
window_height = 700
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
self.root.geometry(f"{window_width}x{window_height}+{x}+{y}")
self.root.minsize(950, 650)
# 初始化
self.monitor = DroidMonitor()
self.auto_refresh = False
self.selected_key_id = None
self.refresh_thread = None
self.create_widgets()
self.refresh_keys()
def create_widgets(self):
"""创建界面"""
self.root.grid_rowconfigure(0, weight=1)
self.root.grid_columnconfigure(0, weight=1)
# 主框架
main = ctk.CTkFrame(self.root)
main.grid(row=0, column=0, sticky="nsew", padx=8, pady=8)
main.grid_rowconfigure(1, weight=3)
main.grid_rowconfigure(2, weight=1)
main.grid_columnconfigure(0, weight=2)
main.grid_columnconfigure(1, weight=1)
# 标题
title_label = ctk.CTkLabel(
main,
text="Droid Monitor - 余额监控工具",
font=("Microsoft YaHei UI", 18, "bold")
)
title_label.grid(row=0, column=0, columnspan=2, padx=10, pady=(10, 15), sticky="w")
# 左侧:Keys列表
left = ctk.CTkFrame(main)
left.grid(row=1, column=0, padx=(10, 5), pady=(0, 10), sticky="nsew")
left.grid_rowconfigure(1, weight=1)
left.grid_columnconfigure(0, weight=1)
keys_title = ctk.CTkLabel(
left,
text="API Keys 列表",
font=("Microsoft YaHei UI", 14, "bold")
)
keys_title.grid(row=0, column=0, padx=10, pady=(10, 5), sticky="w")
self.keys_scroll = ctk.CTkScrollableFrame(left, fg_color=("gray85", "gray25"))
self.keys_scroll.grid(row=1, column=0, padx=10, pady=(0, 10), sticky="nsew")
self.keys_scroll.grid_columnconfigure(0, weight=1)
# 按钮区域
btn_frame = ctk.CTkFrame(left, fg_color="transparent")
btn_frame.grid(row=2, column=0, padx=10, pady=(0, 10), sticky="ew")
btn_frame.grid_columnconfigure((0, 1, 2), weight=1)
# 第一行按钮
ctk.CTkButton(
btn_frame, text="添加", command=self.add_key, height=32,
font=("Microsoft YaHei UI", 11)
).grid(row=0, column=0, padx=3, pady=2, sticky="ew")
ctk.CTkButton(
btn_frame, text="批量导入", command=self.batch_import, height=32,
font=("Microsoft YaHei UI", 11)
).grid(row=0, column=1, padx=3, pady=2, sticky="ew")
ctk.CTkButton(
btn_frame, text="编辑", command=self.edit_key, height=32,
font=("Microsoft YaHei UI", 11)
).grid(row=0, column=2, padx=3, pady=2, sticky="ew")
# 第二行按钮
ctk.CTkButton(
btn_frame, text="删除", command=self.remove_key, height=32,
fg_color="#c62828", hover_color="#b71c1c",
font=("Microsoft YaHei UI", 11)
).grid(row=1, column=0, padx=3, pady=2, sticky="ew")
ctk.CTkButton(
btn_frame, text="切换", command=self.switch_key, height=32,
font=("Microsoft YaHei UI", 11)
).grid(row=1, column=1, padx=3, pady=2, sticky="ew")
ctk.CTkButton(
btn_frame, text="刷新余额", command=self.refresh_balance, height=32,
font=("Microsoft YaHei UI", 11)
).grid(row=1, column=2, padx=3, pady=2, sticky="ew")
# 第三行按钮
ctk.CTkButton(
btn_frame, text="高级设置", command=self.open_advanced_settings, height=32,
font=("Microsoft YaHei UI", 11)
).grid(row=2, column=0, padx=3, pady=2, sticky="ew")
# 右侧:控制面板
right = ctk.CTkFrame(main)
right.grid(row=1, column=1, padx=(5, 10), pady=(0, 10), sticky="nsew")
right.grid_rowconfigure(2, weight=1)
right.grid_columnconfigure(0, weight=1)
# 状态信息
status_title = ctk.CTkLabel(
right,
text="状态信息",
font=("Microsoft YaHei UI", 13, "bold")
)
status_title.grid(row=0, column=0, padx=10, pady=(10, 5), sticky="w")
self.status_text = ctk.CTkTextbox(
right,
height=120,
font=("Microsoft YaHei UI", 10)
)
self.status_text.grid(row=1, column=0, padx=10, pady=(0, 15), sticky="ew")
# 自动刷新控制
refresh_title = ctk.CTkLabel(
right,
text="自动刷新余额",
font=("Microsoft YaHei UI", 13, "bold")
)
refresh_title.grid(row=2, column=0, padx=10, pady=(5, 5), sticky="w")
refresh_frame = ctk.CTkFrame(right, fg_color="transparent")
refresh_frame.grid(row=3, column=0, padx=10, pady=(0, 10), sticky="ew")
interval_frame = ctk.CTkFrame(refresh_frame, fg_color="transparent")
interval_frame.pack(fill="x", pady=(0, 10))
ctk.CTkLabel(
interval_frame,
text="刷新间隔(秒):",
font=("Microsoft YaHei UI", 11)
).pack(side="left")
self.interval_entry = ctk.CTkEntry(interval_frame, width=70, height=28)
self.interval_entry.pack(side="left", padx=5)
self.interval_entry.insert(0, "30")
self.refresh_btn = ctk.CTkButton(
refresh_frame,
text="启动自动刷新",
command=self.toggle_auto_refresh,
height=36,
fg_color="#2e7d32",
hover_color="#1b5e20",
font=("Microsoft YaHei UI", 12)
)
self.refresh_btn.pack(fill="x", pady=(0, 8))
self.refresh_status = ctk.CTkLabel(
refresh_frame,
text="状态: 未运行",
font=("Microsoft YaHei UI", 10)
)
self.refresh_status.pack()
self.last_refresh_label = ctk.CTkLabel(
refresh_frame,
text="上次刷新: --",
font=("Microsoft YaHei UI", 9),
text_color="gray"
)
self.last_refresh_label.pack(pady=(5, 0))
# 日志区域
log_frame = ctk.CTkFrame(main)
log_frame.grid(row=2, column=0, columnspan=2, padx=10, pady=(0, 10), sticky="nsew")
log_frame.grid_rowconfigure(1, weight=1)
log_frame.grid_columnconfigure(0, weight=1)
log_title = ctk.CTkLabel(
log_frame,
text="运行日志",
font=("Microsoft YaHei UI", 13, "bold")
)
log_title.grid(row=0, column=0, padx=10, pady=(8, 5), sticky="w")
self.log_text = ctk.CTkTextbox(
log_frame,
height=130,
font=("Consolas", 9)
)
self.log_text.grid(row=1, column=0, padx=10, pady=(0, 10), sticky="nsew")
def create_key_card(self, key, idx):
"""创建Key卡片"""
card = ctk.CTkFrame(self.keys_scroll, corner_radius=6)
card.grid(row=idx, column=0, sticky="ew", pady=2, padx=2)
card.grid_columnconfigure(1, weight=1)
current = self.monitor.config.get_current_key()
is_current = current and key.id == current.id
# 编号
num_label = ctk.CTkLabel(
card,
text=f"#{idx+1}",
font=("Microsoft YaHei UI", 14, "bold"),
text_color="#4caf50" if is_current else "gray",
width=35
)
num_label.grid(row=0, column=0, rowspan=2, padx=(6, 4), pady=6)
# 名称
name_label = ctk.CTkLabel(
card,
text=key.name,
font=("Microsoft YaHei UI", 11, "bold"),
anchor="w"
)
name_label.grid(row=0, column=1, sticky="w", padx=4, pady=(6, 1))
# API Key
masked = f"{key.key[:18]}..." if len(key.key) > 18 else key.key
key_label = ctk.CTkLabel(
card,
text=masked,
font=("Consolas", 8),
text_color="gray",
anchor="w"
)
key_label.grid(row=1, column=1, sticky="w", padx=4, pady=(0, 6))
# 余额
if key.balance:
bal_m = key.balance.remaining / 1_000_000
bal_text = f"{bal_m:.1f}M"
if hasattr(key.balance, 'change') and key.balance.change != 0:
ch_m = abs(key.balance.change) / 1_000_000
if key.balance.change > 0:
bal_text += f" ↑{ch_m:.1f}M"
else:
bal_text += f" ↓{ch_m:.1f}M"
bal_label = ctk.CTkLabel(
card,
text=bal_text,
font=("Microsoft YaHei UI", 10),
width=85
)
bal_label.grid(row=0, column=2, rowspan=2, padx=(4, 6), pady=6)
if is_current:
card.configure(border_width=2, border_color="#4caf50")
def select(e=None):
self.selected_key_id = key.id
self.log(f"已选择: {key.name}")
self.refresh_keys()
for w in [card, num_label, name_label, key_label]:
w.bind("<Button-1>", select)
def refresh_keys(self):
"""刷新Keys列表"""
for w in self.keys_scroll.winfo_children():
w.destroy()
for i, key in enumerate(self.monitor.config.api_keys):
self.create_key_card(key, i)
self.update_status()
def update_status(self):
"""更新状态"""
s = self.monitor.get_status()
info = (
f"总Keys数: {s['total_keys']}\n"
f"当前Key: {s['current_key'] or '无'}\n"
f"自动刷新: {'运行中' if s['monitoring'] else '未运行'}\n"
f"\n"
f"总余额: {s['total_balance']['remaining']:,.0f} tokens\n"
f"使用率: {s['total_balance']['used_ratio']*100:.1f}%"
)
self.status_text.delete("0.0", "end")
self.status_text.insert("0.0", info)
def log(self, msg):
"""记录日志"""
ts = datetime.now().strftime("%H:%M:%S")
entry = f"[{ts}] {msg}\n"
self.log_text.insert("end", entry)
self.log_text.see("end")
print(f"[日志] {msg}")
def add_key(self):
"""添加Key"""
self.log("操作: 添加Key")
dlg = ctk.CTkToplevel(self.root)
dlg.title("添加 API Key")
dlg.geometry("450x200")
dlg.transient(self.root)
dlg.grab_set()
x = (dlg.winfo_screenwidth()//2) - 225
y = (dlg.winfo_screenheight()//2) - 100
dlg.geometry(f"+{x}+{y}")
ctk.CTkLabel(
dlg, text="名称:",
font=("Microsoft YaHei UI", 12)
).pack(anchor="w", padx=20, pady=(20, 5))
name_e = ctk.CTkEntry(dlg, width=410, height=32)
name_e.pack(padx=20, pady=(0, 10))
ctk.CTkLabel(
dlg, text="API Key:",
font=("Microsoft YaHei UI", 12)
).pack(anchor="w", padx=20, pady=(5, 5))
key_e = ctk.CTkEntry(dlg, width=410, height=32)
key_e.pack(padx=20, pady=(0, 15))
def save():
name = name_e.get().strip()
key = key_e.get().strip()
if not name or not key:
messagebox.showwarning("警告", "请填写完整信息")
return
try:
self.monitor.add_key(name, key)
self.log(f"已添加: {name}")
self.refresh_keys()
dlg.destroy()
messagebox.showinfo("成功", f"已添加: {name}")
except Exception as e:
self.log(f"添加失败: {e}")
messagebox.showerror("错误", str(e))
bf = ctk.CTkFrame(dlg, fg_color="transparent")
bf.pack(pady=8)
ctk.CTkButton(
bf, text="保存", command=save, width=100, height=32,
font=("Microsoft YaHei UI", 11)
).pack(side="left", padx=5)
ctk.CTkButton(
bf, text="取消", command=dlg.destroy, width=100, height=32,
fg_color="gray", font=("Microsoft YaHei UI", 11)
).pack(side="left", padx=5)
def batch_import(self):
"""批量导入"""
self.log("操作: 批量导入")
dlg = ctk.CTkToplevel(self.root)
dlg.title("批量导入 API Keys")
dlg.geometry("700x500")
dlg.transient(self.root)
dlg.grab_set()
x = (dlg.winfo_screenwidth()//2) - 350
y = (dlg.winfo_screenheight()//2) - 250
dlg.geometry(f"+{x}+{y}")
info = (
"格式说明 (每行一个):\n"
"1. 仅Key (自动命名): fk-xxx...\n"
"2. 逗号分隔: 主要Key,fk-xxx...\n"
"3. 竖线分隔: 主要Key|fk-xxx..."
)
ctk.CTkLabel(
dlg, text=info,
font=("Microsoft YaHei UI", 11),
justify="left"
).pack(anchor="w", padx=20, pady=15)
txt = ctk.CTkTextbox(dlg, font=("Consolas", 10))
txt.pack(fill="both", expand=True, padx=20, pady=(0, 10))
def do_import():
self.log("开始导入...")
content = txt.get("0.0", "end").strip()
if not content:
messagebox.showwarning("警告", "没有内容")
return
lines = content.split('\n')
ok = fail = 0
for line in lines:
line = line.strip()
if not line or line.startswith('#'):
continue
try:
name = key = None
if ',' in line:
name, key = [x.strip() for x in line.split(',', 1)]
elif '|' in line:
name, key = [x.strip() for x in line.split('|', 1)]
else:
key = line.strip()
name = f"Key {len(self.monitor.config.api_keys)+1}"
if not key or not key.startswith('fk-'):
self.log(f"跳过无效: {line[:25]}...")
fail += 1
continue
if any(k.key == key for k in self.monitor.config.api_keys):
self.log(f"跳过重复: {name}")
fail += 1
continue
self.monitor.add_key(name, key)
self.log(f"已导入: {name}")
ok += 1
except Exception as e:
self.log(f"导入错误: {e}")
fail += 1
self.refresh_keys()
messagebox.showinfo("导入结果", f"成功: {ok}\n失败: {fail}")
if ok > 0:
dlg.destroy()
bf = ctk.CTkFrame(dlg, fg_color="transparent")
bf.pack(pady=10)
ctk.CTkButton(
bf, text="开始导入", command=do_import, width=120, height=36,
font=("Microsoft YaHei UI", 12)
).pack(side="left", padx=5)
ctk.CTkButton(
bf, text="取消", command=dlg.destroy, width=120, height=36,
fg_color="gray", font=("Microsoft YaHei UI", 12)
).pack(side="left", padx=5)
def edit_key(self):
"""编辑Key"""
if not self.selected_key_id:
self.log("未选择Key")
messagebox.showwarning("警告", "请先选择一个Key")
return
key = None
for k in self.monitor.config.api_keys:
if k.id == self.selected_key_id:
key = k
break
if not key:
return
self.log(f"操作: 编辑 {key.name}")
dlg = ctk.CTkToplevel(self.root)
dlg.title("编辑 API Key")
dlg.geometry("450x200")
dlg.transient(self.root)
dlg.grab_set()
x = (dlg.winfo_screenwidth()//2) - 225
y = (dlg.winfo_screenheight()//2) - 100
dlg.geometry(f"+{x}+{y}")
ctk.CTkLabel(
dlg, text="名称:",
font=("Microsoft YaHei UI", 12)
).pack(anchor="w", padx=20, pady=(20, 5))
name_e = ctk.CTkEntry(dlg, width=410, height=32)
name_e.pack(padx=20, pady=(0, 10))
name_e.insert(0, key.name)
ctk.CTkLabel(
dlg, text="API Key:",
font=("Microsoft YaHei UI", 12)
).pack(anchor="w", padx=20, pady=(5, 5))
key_e = ctk.CTkEntry(dlg, width=410, height=32)
key_e.pack(padx=20, pady=(0, 15))
key_e.insert(0, key.key)
def save():
new_name = name_e.get().strip()
new_key = key_e.get().strip()
if not new_name or not new_key:
messagebox.showwarning("警告", "请填写完整信息")
return
try:
key.name = new_name
if key.key != new_key:
key.key = new_key
key.balance = None
self.monitor.save_config()
self.log(f"已更新: {new_name}")
self.refresh_keys()
dlg.destroy()
messagebox.showinfo("成功", "已更新")
except Exception as e:
self.log(f"更新失败: {e}")
messagebox.showerror("错误", str(e))
bf = ctk.CTkFrame(dlg, fg_color="transparent")
bf.pack(pady=8)
ctk.CTkButton(
bf, text="保存", command=save, width=100, height=32,
font=("Microsoft YaHei UI", 11)
).pack(side="left", padx=5)
ctk.CTkButton(
bf, text="取消", command=dlg.destroy, width=100, height=32,
fg_color="gray", font=("Microsoft YaHei UI", 11)
).pack(side="left", padx=5)
def remove_key(self):
"""删除Key"""
if not self.selected_key_id:
self.log("未选择Key")
messagebox.showwarning("警告", "请先选择一个Key")
return
key = None
for k in self.monitor.config.api_keys:
if k.id == self.selected_key_id:
key = k
break
if not key:
return
self.log(f"操作: 删除 {key.name}")
if messagebox.askyesno("确认", f"确定删除 '{key.name}' 吗?"):
try:
self.monitor.remove_key(key.id)
self.log(f"已删除: {key.name}")
self.selected_key_id = None
self.refresh_keys()
messagebox.showinfo("成功", "已删除")
except Exception as e:
self.log(f"删除失败: {e}")
messagebox.showerror("错误", str(e))
def switch_key(self):
"""切换Key"""
if not self.selected_key_id:
self.log("未选择Key")
messagebox.showwarning("警告", "请先选择一个Key")
return
key = None
for k in self.monitor.config.api_keys:
if k.id == self.selected_key_id:
key = k
break
if not key:
return
self.log(f"操作: 切换到 {key.name}")
try:
self.monitor.switch_to_key(key.id)
self.log(f"已切换到: {key.name}")
self.refresh_keys()
messagebox.showinfo("成功", f"已切换到: {key.name}")
except Exception as e:
self.log(f"切换失败: {e}")
messagebox.showerror("错误", str(e))
def refresh_balance(self):
"""刷新余额"""
self.log("操作: 刷新余额")
def fetch():
try:
self.log("正在查询余额...")
balances = self.monitor.fetch_all_balances()
self.root.after(0, lambda: self.on_balance_fetched(balances))
except Exception as e:
self.root.after(0, lambda: self.log(f"查询失败: {e}"))
self.root.after(0, lambda: messagebox.showerror("错误", str(e)))
threading.Thread(target=fetch, daemon=True).start()
def on_balance_fetched(self, balances):
"""余额查询完成"""
self.log("余额查询完成")
# 更新上次刷新时间
now = datetime.now().strftime("%H:%M:%S")
self.last_refresh_label.configure(text=f"上次刷新: {now}")
for key in self.monitor.config.api_keys:
if key.balance and hasattr(key.balance, 'change') and key.balance.change != 0:
ch_m = abs(key.balance.change) / 1_000_000
dir_text = "增加" if key.balance.change > 0 else "减少"
self.log(f"{key.name}: 余额{dir_text} {ch_m:.1f}M")
self.refresh_keys()
info = "余额信息:\n\n"
for name, bal in balances.items():
if bal:
m = bal['remaining'] / 1_000_000
info += f"{name}: {m:.1f}M ({bal['used_ratio']*100:.1f}%)\n"
else:
info += f"{name}: 查询失败\n"
messagebox.showinfo("余额信息", info)
def open_advanced_settings(self):
"""高级设置"""
self.log("操作: 打开高级设置")
dlg = ctk.CTkToplevel(self.root)
dlg.title("高级设置 - 全局配置")
dlg.geometry("650x580")
dlg.transient(self.root)
dlg.grab_set()
x = (dlg.winfo_screenwidth()//2) - 325
y = (dlg.winfo_screenheight()//2) - 290
dlg.geometry(f"+{x}+{y}")
# 说明
ctk.CTkLabel(
dlg,
text="全局高级配置\n保存为默认配置,切换Key时自动应用到Factory config.json",
font=("Microsoft YaHei UI", 11),
justify="left",
text_color="gray"
).pack(anchor="w", padx=20, pady=15)
# 表单区域
form = ctk.CTkFrame(dlg, fg_color="transparent")
form.pack(fill="both", expand=True, padx=20, pady=(0, 10))
# 获取当前全局配置
global_config = self.monitor.config.global_advanced_config
# 显示名称
ctk.CTkLabel(
form, text="显示名称:",
font=("Microsoft YaHei UI", 11)
).pack(anchor="w", pady=(5, 3))
display_name_var = ctk.StringVar(value=global_config.display_name)
display_name_e = ctk.CTkEntry(form, textvariable=display_name_var, height=32)
display_name_e.pack(fill="x", pady=(0, 10))
# 模型
ctk.CTkLabel(
form, text="模型:",
font=("Microsoft YaHei UI", 11)
).pack(anchor="w", pady=(5, 3))
model_var = ctk.StringVar(value=global_config.model)
model_combo = ctk.CTkComboBox(
form,
values=["claude-sonnet-4-5-20250929", "claude-opus-4-1-20250805"],
variable=model_var,
height=32
)
model_combo.pack(fill="x", pady=(0, 10))
# 基础URL
ctk.CTkLabel(
form, text="API Base URL:",
font=("Microsoft YaHei UI", 11)
).pack(anchor="w", pady=(5, 3))
base_url_var = ctk.StringVar(value=global_config.base_url)
base_url_e = ctk.CTkEntry(form, textvariable=base_url_var, height=32)
base_url_e.pack(fill="x", pady=(0, 10))
# 提供商
ctk.CTkLabel(
form, text="提供商:",
font=("Microsoft YaHei UI", 11)
).pack(anchor="w", pady=(5, 3))
provider_var = ctk.StringVar(value=global_config.provider)
provider_combo = ctk.CTkComboBox(
form,
values=["anthropic", "openai"],
variable=provider_var,
height=32,
state="readonly"
)
provider_combo.pack(fill="x", pady=(0, 15))
# 提示
ctk.CTkLabel(
form,
text="提示: 这些配置会在切换Key时自动应用到Factory配置文件",
font=("Microsoft YaHei UI", 9),
text_color="gray"
).pack(anchor="w", pady=(5, 0))
def save_settings():
try:
# 更新全局配置
self.monitor.config.global_advanced_config.display_name = display_name_var.get()
self.monitor.config.global_advanced_config.model = model_var.get()
self.monitor.config.global_advanced_config.base_url = base_url_var.get()
self.monitor.config.global_advanced_config.provider = provider_var.get()
# 保存配置
self.monitor.save_config()
self.log("已保存全局高级配置")
messagebox.showinfo("成功", "全局配置已保存!\n下次切换Key时会自动应用到Factory配置")
dlg.destroy()
except Exception as e:
self.log(f"保存配置失败: {e}")
messagebox.showerror("错误", f"保存失败: {e}")
# 按钮
bf = ctk.CTkFrame(dlg, fg_color="transparent")
bf.pack(pady=10)
ctk.CTkButton(
bf, text="保存配置", command=save_settings, width=120, height=38,
font=("Microsoft YaHei UI", 12)
).pack(side="left", padx=5)
ctk.CTkButton(
bf, text="取消", command=dlg.destroy, width=120, height=38,
fg_color="gray", font=("Microsoft YaHei UI", 12)
).pack(side="left", padx=5)
def toggle_auto_refresh(self):
"""切换自动刷新"""
if not self.auto_refresh:
try:
interval = int(self.interval_entry.get())
if interval < 10:
messagebox.showwarning("警告", "刷新间隔不能小于10秒")
return
except ValueError:
messagebox.showwarning("警告", "请输入有效的间隔时间")
return
self.auto_refresh = True
self.refresh_btn.configure(
text="停止自动刷新",
fg_color="#c62828",
hover_color="#b71c1c"
)
self.refresh_status.configure(text="状态: 运行中")
self.log(f"自动刷新已启动 (间隔: {interval}秒)")
def refresh_loop():
while self.auto_refresh:
try:
# 刷新余额
balances = self.monitor.fetch_all_balances()
self.root.after(0, lambda: self.on_auto_refresh_done(balances))
# 等待间隔
for _ in range(interval):
if not self.auto_refresh:
break
threading.Event().wait(1)
except Exception as e:
self.root.after(0, lambda: self.log(f"自动刷新错误: {e}"))
break
self.refresh_thread = threading.Thread(target=refresh_loop, daemon=True)
self.refresh_thread.start()
else:
self.auto_refresh = False
self.refresh_btn.configure(
text="启动自动刷新",
fg_color="#2e7d32",
hover_color="#1b5e20"
)
self.refresh_status.configure(text="状态: 未运行")
self.log("自动刷新已停止")
def on_auto_refresh_done(self, balances):
"""自动刷新完成"""
now = datetime.now().strftime("%H:%M:%S")
self.last_refresh_label.configure(text=f"上次刷新: {now}")
self.log(f"自动刷新完成 [{now}]")
# 检查余额变化
for key in self.monitor.config.api_keys:
if key.balance and hasattr(key.balance, 'change') and key.balance.change != 0:
ch_m = abs(key.balance.change) / 1_000_000
dir_text = "增加" if key.balance.change > 0 else "减少"
self.log(f" {key.name}: {dir_text} {ch_m:.1f}M")
self.refresh_keys()
def main():
root = ctk.CTk()
app = DroidMonitorGUI(root)
root.mainloop()
if __name__ == "__main__":
main()