首页 > 解决方案 > 使用 Python/pystray,如何通过单击从系统托盘图标最大化窗口?

问题描述

这篇文章之后,我实现了将应用程序的窗口最小化到系统托盘。

但是,我不知道如何通过单击系统的托盘图标来恢复窗口。只能打开菜单并选择“显示”选项。单击该图标,会引发 TypeError:

File "C:\<path>\pystray\_base.py", line 106, in __call__
self._menu(self)
TypeError: 'tuple' object is not callable

对我有什么提示吗?谢谢

标签: pythonwindowstkintersystem-traypystray

解决方案


您需要为menu选项分配一个实例pystray.Menu而不是传递一个元组:

...
from pystray import Menu, MenuItem, Icon
...
def withdraw_window():  
    window.withdraw()
    image = Image.open("icon.png")
    # create instance of pystray.Menu instead of a tuple
    menu = Menu(
        MenuItem('Quit', quit_window),
        MenuItem('Show', show_window, default=True) # set 'Show' as the default action
    )
    icon = Icon("name", icon=image, title="title", menu=menu)
    icon.run()
...

推荐阅读