Tkinter:App.on_select() 缺少一个必需的位置参数:’event’

huangapple go评论96阅读模式
英文:

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: &#39;event&#39;

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(&quot;App&quot;)
self.tabControl = ttk.Notebook(master)
self.tabOr = ttk.Frame(self.tabControl, style=&#39;My.TFrame&#39;)
self.tabOptions = ttk.Frame(self.tabControl, style=&#39;My.TFrame&#39;)
self.tabControl.add(self.tabOr, text=&#39;Or&#39;)
self.tabControl.add(self.tabOptions, text=&#39;Options&#39;)
self.tabControl.pack(expand=1, fill=&#39;both&#39;)
self.create_dropdown_gen2(self.tabOr)
self.create_picture_frame(self.tabOr)
def create_dropdown_gen2(self, tab):
options = [&quot;Add&quot;,&quot;Platypus&quot;,&quot;Items&quot;]
tk.Label(tab, text=&quot;Pokemon :&quot;).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=&quot;readonly&quot;)
combobox.place(x=450,y=140)
combobox.bind(&quot;&lt;&lt;ComboboxSelected&gt;&gt;&quot;, RadianceGBA.on_select)
def create_picture_frame(self, tab, selected=None):
if selected is None:
selected = &quot;placeholder&quot;
image_path = &quot;Pictures/&quot;+selected+&quot;.png&quot;
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(&quot;640x420&quot;)
root.minsize(640,420)
root.maxsize(640,420)
root.style = ttk.Style()
root.style.theme_use(&quot;clam&quot;)
app = RadianceGBA(root)
root.mainloop()

If anyone has any idea why this error occurs, i would be grateful for those.

答案1

得分: 1

按照原文,on_select 方法接受两个参数:selfevent。但是你的事件绑定存在问题。你需要将

combobox.bind("<<ComboboxSelected>>", RadianceGBA.on_select)

改为

combobox.bind("<<ComboboxSelected>>", self.on_select)

这样,self 参数就会正确地传递给事件处理程序,同时还有 event 对象。

注意:在类内部引用类方法的任何地方,你都可以在方法前面加上 self.;例如,self.create_picture_frameself.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(&quot;&lt;&lt;ComboboxSelected&gt;&gt;&quot;, RadianceGBA.on_select)

to

combobox.bind(&quot;&lt;&lt;ComboboxSelected&gt;&gt;&quot;, 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.

huangapple
  • 本文由 发表于 2023年3月7日 04:18:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定