Reputation: 33
I'm new in python/tkinter/pystray. Used following code sample for my test application, run it on Windows 10:
# Import the required libraries
from tkinter import *
from pystray import MenuItem as item
import pystray
from PIL import Image, ImageTk
# Create an instance of tkinter frame or window
win=Tk()
win.title("System Tray Application")
# Set the size of the window
win.geometry("700x350")
# Define a function for quit the window
def quit_window(icon, item):
icon.stop()
win.destroy()
# Define a function to show the window again
def show_window(icon, item):
icon.stop()
win.after(0,win.deiconify())
# Hide the window and show on the system taskbar
def hide_window():
win.withdraw()
image=Image.open("favicon.ico")
menu=(item('Quit', quit_window), item('Show', show_window))
icon=pystray.Icon("name", image, "My System Tray Icon", menu)
icon.run()
win.protocol('WM_DELETE_WINDOW', hide_window)
win.mainloop()
On restoring window from sys tray there following error messages take place:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\kapustin.av\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "C:\Users\kapustin.av\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 839, in callit
func(*args)
TypeError: 'str' object is not callable
I suppose the issue is around show_window
method.
Once I changed it in this way:
def show_window(icon, item):
icon.stop()
#win.after(0, win.deiconify())
win.deiconify()
then error messages are eliminated. At the same time I'm not sure that it really fixed initial issue: while debugging through tkinter code for some time, I didnt get what is really caused those messages.
Would you give an idea about:
win.deiconify()
instead of win.after(0, win.deiconify())
? Sure that win.after
was used here due to some reason.Upvotes: 0
Views: 143