2017-07-02 88 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