-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
89 lines (71 loc) · 2.18 KB
/
main.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2024/11/13 23:16
@Author : SiYuan
@Email : [email protected]
@File : TraceBoard-main.py
@Description :
"""
import os
import threading
import webbrowser
from pystray import Icon, MenuItem, Menu
from PIL import Image, ImageDraw
from uvicorn import Config, Server
from listener.keyboard import start_listener
from log import logger
from server import app, static_dir
# 启动键盘监听的同时运行FastAPI服务器
def start_api():
# import uvicorn
# uvicorn.run(app, host="127.0.0.1", port=21315)
log_config = {
"version": 1,
"disable_existing_loggers": True,
"handlers": {
"file_handler": {
"class": "logging.FileHandler",
"filename": "app.log",
},
},
"root": {
"handlers": ["file_handler"],
"level": "ERROR",
},
}
config = Config(app=app, host="127.0.0.1", port=21315, log_config=log_config)
server = Server(config=config)
server.run()
# 创建托盘图标图像
def create_image(width: int, height: int, color1, color2):
image = Image.new("RGB", (width, height), color1)
dc = ImageDraw.Draw(image)
dc.rectangle(
[(width // 4, height // 4), (width * 3 // 4, height * 3 // 4)], fill=color2
)
image = Image.open(os.path.join(static_dir, 'logo3.0.ico'))
return image
# 托盘图标菜单
def setup_tray_icon():
icon_image = create_image(64, 64, "black", "white")
tray_icon = Icon("Keyboard Monitor", icon_image, '打开统计面板', menu=Menu(
MenuItem("查看统计", open_dashboard),
MenuItem("退出软件", exit_app)
))
tray_icon.run()
# 打开前端 HTML 页面
def open_dashboard(icon, item):
webbrowser.open("http://127.0.0.1:21315/")
# 退出程序
def exit_app(icon, item):
icon.stop()
os._exit(0)
# 主线程启动
if __name__ == "__main__":
# 启动键盘监听器和 API 服务器
threading.Thread(target=start_listener).start()
threading.Thread(target=start_api).start()
webbrowser.open("http://127.0.0.1:21315/")
# 设置托盘图标
setup_tray_icon()