【问题标题】:With Python/pystray, how to maximize a window from the system tray icon by clicking it?使用 Python/pystray,如何通过单击从系统托盘图标最大化窗口?
【发布时间】:2021-11-16 14:12:12
【问题描述】:

按照this post,我已经实现了将应用程序的窗口最小化到系统托盘。

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

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

有什么提示吗? 谢谢

【问题讨论】:

  • 它告诉你self._menu 是一个元组,你显然不能调用一个元组,例如self._menu = (1, 2, 3),你尝试做(1, 2, 3)()
  • 我明白了。但是,右键单击图标可显示菜单,但左键单击无效。在代码中,“menu”变量被定义为一个带有菜单选项的元组。

标签: python windows tkinter system-tray pystray


【解决方案1】:

您需要为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()
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多