程序结束时如何销毁图标托盘

2024-05-23 20:59:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个类,它在.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{a1},我做得很正确:

To destroy the icon when the program ends, call systray.shutdown()

所以我不确定我错过了什么。。。有人能帮忙吗?谢谢


Tags: inpyselfdeflineusersquitfile