0

通常,当我在没有面向对象的情况下编写 tkinter 程序时,例如:

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

我没有收到任何错误。它正在工作。但是当我使用面向对象的程序编写程序时,例如:

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

当我点击退出时。我收到以下错误:

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

正如我在上面的两个示例中所示,问题源于 pystray 包。有人可以帮忙吗?

4

1 回答 1

0

好的。我解决了这个问题。我分享我的代码以帮助他人。只需使用 lambda 函数:

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()
于 2019-03-18T15:36:32.530 回答