Python Tkinter窗口在从系统托盘恢复后挂起(使用Pyroster)
我正在用Python创建一个基于Tkinter的GUI。我希望窗口在最小化时隐藏到系统托盘(使用Python Tkinter窗口在从系统托盘恢复后挂起(使用Pyroster),python,tkinter,Python,Tkinter,我正在用Python创建一个基于Tkinter的GUI。我希望窗口在最小化时隐藏到系统托盘(使用模块)。它隐藏了,但只有在我尝试恢复它时,它才会出现在屏幕上并挂起 以下是我尝试过的: from tkinter import * from PIL import Image import pystray def hide_to_tray(_event=None): tray_icon = pystray.Icon("MyTrayIcon", title="My tray icon")
模块)。它隐藏了,但只有在我尝试恢复它时,它才会出现在屏幕上并挂起
以下是我尝试过的:
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()
从tkinter导入*
从PIL导入图像
进口猪笼草
def隐藏到托盘(_事件=无):
托盘图标=pytrayicon.icon(“MyTrayIcon”,title=“我的托盘图标”)#创建托盘图标
tray_icon.icon=Image.open(“app_icon.ico”)#使用PIL打开图标
tray_icon.menu=Pyrostate.menu(Pyrostate.MenuItem(“打开”,lambda:tray_icon.stop(),默认值=True))35;创建菜单
root.draw()#隐藏窗口
托盘_icon.run()#运行图标的主循环
#图标主循环
root.deiconify()#当图标mainloop停止时,再次显示窗口
root.focus_force()#专注于它
root=Tk()
btn=按钮(根,text=“示例按钮”)
btn.grid()
root.bind(“,隐藏到托盘)#隐藏到托盘
root.mainloop()
如何解决这个问题?1。使用
它在单独的线程上运行,因此不会阻塞,请使用pip安装:
pip install infi.systray
不要从info.systray
线程调用tkinter方法:
由于infi.systray
在单独的线程上运行,因此在创建时不能在传递给systray图标的回调函数中直接调用tkinter方法。使用线程安全的方式(例如)将systray图标中的事件通知主线程
2.不要同时运行pytraste
和tkinter
您不能同时运行它们,因为它们会阻止正在运行的线程,并且两者都必须在主线程上运行。
请注意,只有在tkinter应用程序关闭时才会显示系统托盘图标。在主线程之外的另一个线程中运行tkinter或PyTrastice,然后通过队列相互通信。遗憾的是,我认为PyTrastice和tkinter只有在主线程上运行时才能工作