0

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:

  • Is it correct from tkinter/pystray perspective to use win.deiconify() instead of win.after(0, win.deiconify())? Sure that win.after was used here due to some reason.
  • Would it be a case here about tkinter/pystray version, python version, Windows version etc?
Andrey K
  • 23
  • 3
  • Actually `win.after(0, win.deiconify())` will executed `win.deiconify()` immediately and pass the result to `win.after()`. So it is more or less the same as just calling `win.deiconify()` directly. I have no issue when executing your code using both versions in my Windows 7 with Python 3.8.13. – acw1668 Sep 09 '22 at 09:19
  • @acw1668 , thanks. For me it is clear, that formally ```win.after(0, job)``` and just ```job()``` are "sync" calls. I expected, that there would be some tkinter specific, why ```deiconify()``` is called via ```.after(...)```. And thanks for info, that there is no issue with code at your end. May be I have side effect on my environment only. – Andrey K Sep 10 '22 at 09:14

0 Answers0