2017-07-02 121 views
-1

我發現SysTrayIcon.py可以在Windows中輕鬆創建Systray圖標。 問題是,它阻止代碼的進一步執行,作爲一個例子,這是我創造我目前的任務欄圖標:Systray進一步阻止代碼執行

import _tray #This is a simple import of SysTrayIcon.py 

#Create SysTray 
def EXIT(self): None 
def DEBUG(self): print("TEST") 
def SHOW(self): print("Showing Main Window") 
HOVER = "Foo v"+version 
ICON = root+"apr.ico" 
MENUE =(('1', None, DEBUG), 
     ('2', None, SHOW), 
     ('Sub', None, (
      ('Sub 1', None, DEBUG), 
      ('Sub 2', None, DEBUG),))) 

_tray.SysTrayIcon(ICON, HOVER, MENUE, on_quit=EXIT, default_menu_index=1) 

如果我現在加入可以說,這樣的代碼:

print("This get's executed after the Trayicon is quit.") 

到另一個代碼,它不會得到執行,直到我退出Trayicon,我如何避免/解決上述行爲?

回答

1

您可以使用線程將WIN32 API上下文從應用程序邏輯中分離出來。例如,如果你更換了直接調用:

import threading 

def run_systray(icon, hover, menu, **options): 
    _tray.SysTrayIcon(icon, hover, menu, **options) 

thread = threading.Thread(target=run_systray, 
          args=(ICON, HOVER, MENUE), 
          kwargs={"on_quit": EXIT, "default_menu_index": 1}) 
thread.start() 

print("This gets executed immediately...") 

# you can do whatever you want here 

# in the end, lets cleanly handle the thread closing: 
thread.join() 

print("This gets executed only after systray exit...") 

SysTrayIcon類會很樂意與WIN32 API聊天,而直到你決定加入該線程返回到主一個阻塞代碼的其餘部分。

+0

正是我想要的,非常感謝! – Lyux