Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Systray阻止进一步执行代码_Python_Multithreading_Python 3.x_System Tray - Fatal编程技术网

Python Systray阻止进一步执行代码

Python Systray阻止进一步执行代码,python,multithreading,python-3.x,system-tray,Python,Multithreading,Python 3.x,System Tray,我发现可以在Windows中轻松创建Systray图标。 问题是,它阻碍了代码的进一步执行,例如,我就是这样创建当前trayicon的: 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 = "Fo

我发现可以在Windows中轻松创建Systray图标。 问题是,它阻碍了代码的进一步执行,例如,我就是这样创建当前trayicon的:

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之前它不会执行,我如何避免/修复所述行为?

您可以使用线程将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聊天,而不会阻塞其余代码,直到您决定将线程连接回主线程