-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.py
More file actions
189 lines (152 loc) · 6.12 KB
/
Copy pathmain.py
File metadata and controls
189 lines (152 loc) · 6.12 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
"""
Main entry point for the OCR translator application.
"""
import sys
import os
import platform
from PIL import ImageGrab
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QMenu, QAction, QStyle
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QTimer
from config_manager import config
from core.ocr_translator import OCRTranslator
def register_hotkey(hotkeys, translator):
"""Register global hotkeys using Windows RegisterHotKey."""
try:
# 1 = screenshot, 2 = AI 学习
hotkeys.register(1, config.SCREENSHOT_HOTKEY, translator.start_translation)
ai_hotkey = config.get('HOTKEYS', 'AI_STUDY_HOTKEY', 'ctrl+alt+x')
hotkeys.register(2, ai_hotkey, translator.show_ai_study)
print(f"已注册全局热键: {config.SCREENSHOT_HOTKEY},AI学习: {ai_hotkey}")
except Exception as e:
print(f"注册系统热键失败: {e}")
def check_hotkey(_translator):
"""使用RegisterHotKey后无需定时自检。"""
return
def main():
"""
Main function to start the OCR translator application.
"""
# 使用系统级热键,无需 keyboard 句柄
# 创建 QApplication 实例
app = QApplication(sys.argv)
# 设置应用程序信息
app.setApplicationName("OCR Translator")
app.setQuitOnLastWindowClosed(False) # 关闭窗口时不退出应用
# 创建 OCRTranslator 实例
translator = OCRTranslator()
# 提前定义变量,供后续闭包引用
hotkeys = None
# 创建系统托盘图标
tray_icon = QSystemTrayIcon()
# 使用自定义图标
if getattr(sys, 'frozen', False):
# 如果是EXE,使用EXE所在目录
base_path = os.path.dirname(sys.executable)
else:
# 如果是开发环境,使用当前文件所在目录
base_path = os.path.dirname(os.path.abspath(__file__))
icon_path = os.path.join(base_path, "assets", "icon.png")
if os.path.exists(icon_path):
icon = QIcon(icon_path)
tray_icon.setIcon(icon)
else:
# 如果自定义图标不存在,使用内置图标
print(f"警告: 找不到图标文件 {icon_path},使用内置图标")
# QStyle 已导入,防止之前的未定义错误
icon = app.style().standardIcon(QStyle.SP_ComputerIcon)
tray_icon.setIcon(icon)
tray_icon.setToolTip("OCR Translator")
# 创建托盘菜单
menu = QMenu()
# 添加翻译动作
translate_action = QAction("开始翻译")
translate_action.triggered.connect(translator.start_translation)
menu.addAction(translate_action)
# 添加查看历史记录动作
history_action = QAction("查看历史记录")
history_action.triggered.connect(translator.show_history)
menu.addAction(history_action)
# 添加AI学习动作
ai_study_action = QAction("AI学习")
ai_study_action.triggered.connect(translator.show_ai_study)
menu.addAction(ai_study_action)
# 学习游戏:Match / Cloze / Shadow
game_action = QAction("开始学习游戏 · 配对(15秒)")
game_action.triggered.connect(translator.show_game)
menu.addAction(game_action)
cloze_action = QAction("开始 Cloze 爆破")
cloze_action.triggered.connect(translator.show_game_cloze)
menu.addAction(cloze_action)
shadow_action = QAction("开始 影子跟读")
shadow_action.triggered.connect(translator.show_game_shadow)
menu.addAction(shadow_action)
daily_action = QAction("每日 3 分钟任务")
daily_action.triggered.connect(translator.start_daily_mission)
menu.addAction(daily_action)
# (已在上方添加各游戏入口)
# 添加设置动作
settings_action = QAction("设置")
settings_action.triggered.connect(translator.show_settings)
menu.addAction(settings_action)
# 添加重新注册热键动作
reregister_action = QAction("重新注册热键")
reregister_action.triggered.connect(lambda: register_hotkey(hotkeys, translator))
menu.addAction(reregister_action)
# 添加分隔线
menu.addSeparator()
# 添加关于动作
about_action = QAction("关于")
about_action.triggered.connect(translator.show_about)
menu.addAction(about_action)
# 添加分隔线
menu.addSeparator()
# 添加退出动作
exit_action = QAction("退出")
def _quit():
try:
if hotkeys is not None:
hotkeys.unregister_all()
finally:
app.quit()
exit_action.triggered.connect(_quit)
menu.addAction(exit_action)
# 设置托盘菜单
tray_icon.setContextMenu(menu)
tray_icon.show()
# 到期提醒:每隔 20 分钟提示一次(仅当有到期且未显示窗口)
def due_check():
try:
lm = getattr(translator, 'learning_manager', None)
if not lm:
return
due = len(lm.due_items(limit=3))
if due:
tray_icon.showMessage("学习提醒", f"有 {due} 个到期项目,来一局 15 秒?", QSystemTrayIcon.Information, 4000)
except Exception:
pass
due_timer = QTimer()
due_timer.timeout.connect(due_check)
due_timer.start(20 * 60 * 1000)
# 注册全局热键(仅Windows支持)
if platform.system() != 'Windows':
print('警告: 全局热键仅在Windows上受支持,当前平台无法注册。')
hotkeys = None
else:
from utils.global_hotkeys import GlobalHotkeys
hotkeys = GlobalHotkeys(app)
register_hotkey(hotkeys, translator)
# 创建定时器检查热键状态
# 使用 RegisterHotKey 后无需定时检查
# 保留变量名避免引用错误
hotkey_timer = None
# 显示启动消息
tray_icon.showMessage("OCR Translator",
f"OCR翻译器已启动,按 {config.SCREENSHOT_HOTKEY} 开始截图翻译",
QSystemTrayIcon.Information, 3000)
print(f"OCR翻译器已启动,按 {config.SCREENSHOT_HOTKEY} 开始截图翻译")
# 运行应用程序
sys.exit(app.exec_())
if __name__ == "__main__":
main()