0

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?

pigman
  • 43
  • 9
  • 1
    In your original program your function `quit_window` takes two args. In your class you are only passing `self` in your class method. – Henry Yik Mar 18 '19 at 01:52
  • 1
    This answer was not helpful enough. I don't need any more parameters in my second code. – pigman Mar 18 '19 at 14:12
  • Wasn't the error obvious? `quit_window` when called is getting 2 arguments. Although i didn't have `pytray` installed, from the looks of `('Quit', self.quit_window)` it seems to be passing something similar to an `event` in tkinter callback. – Henry Yik Mar 18 '19 at 14:17
  • Ok i solved this problem with lambda function. Thanks. – pigman Mar 18 '19 at 15:36

1 Answers1

1

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()
pigman
  • 43
  • 9