我正在构建一个tkinter gui项目,我正在寻找使用tkinter窗口运行任务栏图标的方法。
我发现Pystray库可以做到这一点,但是现在我想弄清楚如何与tkinter窗口一起使用该库(托盘图标),
当用户退出winodw时,我将其设置为仅退出窗口:
self.protocol('WM_DELETE_WINDOW', self.withdraw)
我想将它带回托盘图标。.有人知道该怎么做吗?
编辑:到目前为止,我到目前为止只编写了这段代码(它们没有一起运行,但是也很好):
from pystray import MenuItem as item
import pystray
from PIL import Image
import tkinter as tk
def quit_window(icon, item):
icon.stop()
#window.destroy()
def show_window(icon, item):
icon.stop()
#window.deiconify()
def withdraw_window(window):
window.withdraw()
image = Image.open("image.ico")
menu = (item('Quit', quit_window), item('Show', show_window))
icon = pystray.Icon("name", image, "title", menu)
icon.run()
def main():
window = tk.Tk()
window.title("Welcome")
window.protocol('WM_DELETE_WINDOW', lambda: withdraw_window(window))
window.mainloop()
main()
答案 0 :(得分:1)
最后我弄清楚了,
现在,我只需要将此代码与我的主要代码结合起来,希望该代码也能对其他人有所帮助...
from pystray import MenuItem as item
import pystray
from PIL import Image
import tkinter as tk
window = tk.Tk()
window.title("Welcome")
def quit_window(icon, item):
icon.stop()
window.destroy()
def show_window(icon, item):
icon.stop()
window.after(0,window.deiconify)
def withdraw_window():
window.withdraw()
image = Image.open("image.ico")
menu = (item('Quit', quit_window), item('Show', show_window))
icon = pystray.Icon("name", image, "title", menu)
icon.run()
window.protocol('WM_DELETE_WINDOW', withdraw_window)
window.mainloop()