Pystray with tkinter TypeError: takes 1 positional argument but 2 were given
Pystray with tkinter TypeError: takes 1 positional argument but 2 were given
通常当我在没有面向对象的情况下编写代码时,我的 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()
我没有得到任何 error.It 的工作。但是当我用面向对象编写我的程序时:
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 包。有人可以帮忙吗?
好的。我解决了这个问题。我分享我的代码来帮助别人。只需使用 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()
通常当我在没有面向对象的情况下编写代码时,我的 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()
我没有得到任何 error.It 的工作。但是当我用面向对象编写我的程序时:
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 包。有人可以帮忙吗?
好的。我解决了这个问题。我分享我的代码来帮助别人。只需使用 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()