【发布时间】:2019-12-05 19:35:04
【问题描述】:
我正在用 Python 创建一个基于 Tkinter 的 GUI。我希望窗口在最小化时隐藏到系统托盘(使用pystray 模块)。它隐藏了,但它只出现在屏幕上并在我尝试恢复它时挂起。
这是我尝试过的:
from tkinter import *
from PIL import Image
import pystray
def hide_to_tray(_event=None):
tray_icon = pystray.Icon("MyTrayIcon", title="My tray icon") # create the tray icon
tray_icon.icon = Image.open("app_icon.ico") # open the icon using PIL
tray_icon.menu = pystray.Menu(pystray.MenuItem("Open", lambda: tray_icon.stop(), default=True)) # create the menu
root.withdraw() # hide the window
tray_icon.run() # run the icon's main loop
# icon mainloop
root.deiconify() # when the icon mainloop had been stopped, show the window again
root.focus_force() # focus on it
root = Tk()
btn = Button(root, text="Sample button")
btn.grid()
root.bind("<Unmap>", hide_to_tray) # hide to tray on minimizing
root.mainloop()
我该如何解决这个问题?
【问题讨论】: