英文:
Tkinter: App.on_select() missing 1 required positional argument: 'event'
问题
我试图开发一个 Python 应用程序,其目标是根据 ComboBox 中选择的值显示特定的图片。
不幸的是,脚本显示以下错误:
return self.func(*args)
^^^^^^^^^^^^^^^^
TypeError: RadianceGBA.on_select() missing 1 required positional argument: 'event'
这是我正在使用的代码:
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
class RadianceGBA:
def __init__(self, master):
self.master = master
master.title("App")
self.tabControl = ttk.Notebook(master)
self.tabOr = ttk.Frame(self.tabControl, style='My.TFrame')
self.tabOptions = ttk.Frame(self.tabControl, style='My.TFrame')
self.tabControl.add(self.tabOr, text='Or')
self.tabControl.add(self.tabOptions, text='Options')
self.tabControl.pack(expand=1, fill='both')
self.create_dropdown_gen2(self.tabOr)
self.create_picture_frame(self.tabOr)
def create_dropdown_gen2(self, tab):
options = ["Add","Platypus","Items"]
tk.Label(tab, text="Pokemon :").place(x=380,y=140)
self.selected_option = tk.StringVar()
self.selected_option.set(options[0])
combobox = ttk.Combobox(tab, values=options, textvariable=self.selected_option, state="readonly")
combobox.place(x=450,y=140)
combobox.bind("<<ComboboxSelected>>", self.on_select)
def create_picture_frame(self, tab, selected=None):
if selected is None:
selected = "placeholder"
image_path = "Pictures/"+selected+".png"
print(image_path)
img = Image.open(image_path).resize((120, 120))
img_tk = ImageTk.PhotoImage(img)
img_label = ttk.Label(tab, image=img_tk)
img_label.image = img_tk
img_label.place(x=460,y=190)
def on_select(self, event):
selected = event.widget.get()
print(selected)
self.create_picture_frame(self.tabOr, selected)
root = tk.Tk()
root.geometry("640x420")
root.minsize(640,420)
root.maxsize(640,420)
root.style = ttk.Style()
root.style.theme_use("clam")
app = RadianceGBA(root)
root.mainloop()
如果有人知道为什么出现这个错误,我将不胜感激。
英文:
I am trying to develop a Python application whose goal is to display
a certain picture depending of the selected value in a ComboBox.
Unluckily the script displays the following error:
return self.func(*args)
^^^^^^^^^^^^^^^^
TypeError: RadianceGBA.on_select() missing 1 required positional argument: 'event'
Here is the code I am using:
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
class RadianceGBA:
def __init__(self, master):
self.master = master
master.title("App")
self.tabControl = ttk.Notebook(master)
self.tabOr = ttk.Frame(self.tabControl, style='My.TFrame')
self.tabOptions = ttk.Frame(self.tabControl, style='My.TFrame')
self.tabControl.add(self.tabOr, text='Or')
self.tabControl.add(self.tabOptions, text='Options')
self.tabControl.pack(expand=1, fill='both')
self.create_dropdown_gen2(self.tabOr)
self.create_picture_frame(self.tabOr)
def create_dropdown_gen2(self, tab):
options = ["Add","Platypus","Items"]
tk.Label(tab, text="Pokemon :").place(x=380,y=140)
self.selected_option = tk.StringVar()
self.selected_option.set(options[0])
combobox = ttk.Combobox(tab, values=options, textvariable=self.selected_option, state="readonly")
combobox.place(x=450,y=140)
combobox.bind("<<ComboboxSelected>>", RadianceGBA.on_select)
def create_picture_frame(self, tab, selected=None):
if selected is None:
selected = "placeholder"
image_path = "Pictures/"+selected+".png"
print(image_path)
img = Image.open(image_path).resize((120, 120))
img_tk = ImageTk.PhotoImage(img)
img_label = ttk.Label(tab, image=img_tk)
img_label.image = img_tk
img_label.place(x=460,y=190)
def on_select(self, event):
selected = event.widget.get()
print(selected)
self.create_picture_frame(self.tabOr, selected)
root = tk.Tk()
root.geometry("640x420")
root.minsize(640,420)
root.maxsize(640,420)
root.style = ttk.Style()
root.style.theme_use("clam")
app = RadianceGBA(root)
root.mainloop()
If anyone has any idea why this error occurs, i would be grateful for those.
答案1
得分: 1
按照原文,on_select
方法接受两个参数:self
和 event
。但是你的事件绑定存在问题。你需要将
combobox.bind("<<ComboboxSelected>>", RadianceGBA.on_select)
改为
combobox.bind("<<ComboboxSelected>>", self.on_select)
这样,self
参数就会正确地传递给事件处理程序,同时还有 event
对象。
注意:在类内部引用类方法的任何地方,你都可以在方法前面加上 self.
;例如,self.create_picture_frame
,self.on_select
等等。解释器会理解你调用的方法存在(或者至少 应该 在类的范围内存在)。
英文:
As written, the on_select
method accepts two arguments: self
and event
. There's a problem with your event binding, however. You'll need to change
combobox.bind("<<ComboboxSelected>>", RadianceGBA.on_select)
to
combobox.bind("<<ComboboxSelected>>", self.on_select)
This way, the self
parameter is passed properly to the event handler along with the event
object.
FYI: Anywhere you're trying to refer to a class method from within that class, you can prefix it with self.
; e.g., self.create_picture_frame
, self.on_select
, etc., and the interpreter will understand that the method you're calling exists (or at least, should exist) within the scope of the class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论