我有一个类,当我的 .py 程序启动时会调用它,它会在 Windows 任务栏中创建一个图标托盘。在其中,有一个选项quit
,它映射到kill_icon_tray
我的类中的函数,它应该终止图标,然后完成我的程序。
这是类(一些方法被省略,因为它们不是必需的):
from infi.systray import SysTrayIcon
class Tray_icon_controller:
def __init__(self):
self.menu_options = (("Open Chat Monitor", None, self.open_chat),)
self.systray = SysTrayIcon("chat.ico", "Engineer Reminder", self.menu_options, on_quit=self.kill_icon_tray);
def init_icon_tray(self):
self.systray.start();
def kill_icon_tray(self, systray):
self.systray.shutdown()
但是,每当我quit
在图标托盘中单击时,都会返回以下异常:
$ py engineer_reminder.py
Traceback (most recent call last):
File "_ctypes/callbacks.c", line 237, in 'calling callback function'
File "C:\Users\i866336\AppData\Local\Programs\Python\Python38-32\lib\site-packages\infi\systray\traybar.py", line 79, in WndProc
self._message_dict[msg](hwnd, msg, wparam.value, lparam.value)
File "C:\Users\i866336\AppData\Local\Programs\Python\Python38-32\lib\site-packages\infi\systray\traybar.py", line 195, in _destroy
self._on_quit(self)
File "C:\Users\i866336\Documents\GitHub\chat_reminder\cl_tray_icon_controller.py", line 17, in kill_icon_tray
self.systray.shutdown()
File "C:\Users\i866336\AppData\Local\Programs\Python\Python38-32\lib\site-packages\infi\systray\traybar.py", line 123, in shutdown
self._message_loop_thread.join()
File "C:\Users\i866336\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 1008, in join
raise RuntimeError("cannot join current thread")
RuntimeError: cannot join current thread
我尝试将方法修改kill_icon_tray
为此,但它引发了相同的异常:
def kill_icon_tray(self, systray):
self.systray.shutdown()
根据infi.systray
文档,我做得正确:
要在程序结束时销毁图标,请调用
systray.shutdown()
所以我不确定我在这里错过了什么......有人可以帮忙吗?谢谢!