Search code examples
pythonpython-3.xooptkintersystray

Pystray with tkinter TypeError: takes 1 positional argument but 2 were given


Normally when I code without objects oriented my tkinter program like:

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()

I'm not getting any error.It's working. But when I code my program with objects oriented like:

from pystray import MenuItem as item
import pystray
from PIL import Image
import tkinter as tk
class Program:
    def __init__(self):
        self.window = tk.Tk()
        self.window.title("Welcome")
        self.window.protocol('WM_DELETE_WINDOW', self.withdraw_window)
        self.window.mainloop()

    def quit_window(self):
        self.icon.stop()
        self.window.destroy()

    def show_window(self):
        self.icon.stop()
        self.window.after(0, self.window.deiconify)

    def withdraw_window(self):
        self.window.withdraw()
        image = Image.open("microphone.ico")
        menu = (item('Quit', self.quit_window), item('Show', self.show_window))
        self.icon = pystray.Icon("name", image, "title", menu)
        self.icon.run()

run=Program()

when I click on the quit. I get the following error:

An error occurred when calling message handler
Traceback (most recent call last):
  File "D:\PythonInterpreter\Python37-32\lib\site-packages\pystray\_win32.py", line 378, in _dispatcher
    uMsg, lambda w, l: 0)(wParam, lParam) or 0)
  File "D:\PythonInterpreter\Python37-32\lib\site-packages\pystray\_win32.py", line 198, in _on_notify
    descriptors[index - 1](self)
  File "D:\PythonInterpreter\Python37-32\lib\site-packages\pystray\_base.py", line 240, in inner
    callback(self)
  File "D:\PythonInterpreter\Python37-32\lib\site-packages\pystray\_base.py", line 327, in __call__
    return self._action(icon, self)
  File "D:\PythonInterpreter\Python37-32\lib\site-packages\pystray\_base.py", line 421, in wrapper1
    return action(icon)
TypeError: quit_window() takes 1 positional argument but 2 were given

As I have shown in the two examples above the problem stems from the pystray package. Can someone help?


Solution

  • Ok. I solved this problem. I share my codes to help others. Just use lambda function:

    from pystray import MenuItem as item
    import pystray
    from PIL import Image
    import tkinter as tk
    class Program:
        def __init__(self):
            self.window = tk.Tk()
            self.window.title("Welcome")
            self.window.protocol('WM_DELETE_WINDOW', self.withdraw_window)
            self.window.mainloop()
    
        def quit_window(self):
            self.icon.stop()
            self.window.destroy()
    
        def show_window(self):
            self.icon.stop()
            self.window.after(0, self.window.deiconify)
    
        def withdraw_window(self):
            self.window.withdraw()
            image = Image.open("microphone.ico")
            menu = (item('Quit', lambda: self.quit_window()), item('Show', lambda: self.show_window()))
            self.icon = pystray.Icon("name", image, "title", menu)
            self.icon.run()
    
    run=Program()