forked from hiroi-sora/Umi-OCR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtray.py
68 lines (56 loc) · 2.5 KB
/
tray.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
from logging import raiseExceptions
from ui.systray.traybar import SysTrayIcon
from utils.asset import Asset
from utils.config import Umi
from utils.config import Config, ClickTrayModeFlag
from ui.win_ocr_language import ChangeOcrLanguage # 更改语言
import atexit # 退出处理
class Tray:
def __init__(self):
self.tray = None
def start(self):
aa = ('显示面板', Asset.getPath('app24ico'), self.showWin)
bb = ('屏幕截图', Asset.getPath('screenshot24ico'), self.screenshot)
cc = ('粘贴图片', Asset.getPath('paste24ico'), self.clipboard)
dd = ('更改语言', Asset.getPath('language24ico'),
lambda *e: ChangeOcrLanguage())
clickTrayMode = Config.get('clickTrayMode').get(
Config.get('clickTrayModeName'), ClickTrayModeFlag.show)
menuOptions = ()
if clickTrayMode == ClickTrayModeFlag.show:
menuOptions = (aa, bb, cc, dd)
elif clickTrayMode == ClickTrayModeFlag.screenshot:
menuOptions = (bb, cc, aa, dd)
elif clickTrayMode == ClickTrayModeFlag.clipboard:
menuOptions = (cc, bb, aa, dd)
self.tray = SysTrayIcon(
Asset.getPath('umiocrico'),
Umi.name, menuOptions,
quit_name='退出',
quit_icon=Asset.getPath('exit24ico'),
on_quit=self.quit)
self.tray.start()
atexit.register(self.stop) # 注册程序终止时执行强制停止子进程
self.main = Config.main
# 注册事件,防止跨线程调用方法
self.main.win.bind(
'<<ClipboardEvent>>', lambda *e: self.main.runClipboard())
# <<ScreenshotEvent>> 在主窗类内已注册
def stop(self):
'''关闭托盘显示的接口。必须在主线程调用,禁止在托盘线程内调用!'''
# https://github.com/Infinidat/infi.systray/issues/26
# https://github.com/Infinidat/infi.systray/issues/32
if not self.tray:
return
self.tray.shutdown()
self.tray = None # 将引用置空,主窗口第二次按下关闭时可强行关闭
def showWin(self, e=None):
self.main.gotoTop(isForce=True)
def screenshot(self, e=None):
self.main.win.event_generate('<<ScreenshotEvent>>')
def clipboard(self, e=None):
self.main.win.event_generate('<<ClipboardEvent>>')
def quit(self, e=None):
if self.main.win:
self.main.win.event_generate('<<QuitEvent>>')
SysTray = Tray() # 托盘单例