Tkinter: App.on_select() missing 1 required positional argument: 'event'

Question:

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.

Asked By: Nicolas VON AESCH

||

Answers:

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.

Answered By: JRiggles