Python 3.8. Why I’m missing 2 required positional arguments: ‘event’ and ‘cmb’?

  combobox, python, python-3.x, tkinter

When I use comboboxes the script prints ok. But when I use button "get list of choices" I receive the error message "get_combo_choice() missing 2 required positional arguments: ‘event’ and ‘cmb’". I can’t figure it out. Thanks for any hint.

import tkinter as tk
from tkinter import  ttk
from tkinter import Tk
from tkinter import Button

root = Tk()
my_heroes = ['Zidane', 'Ronaldo', 'Messi']
position = ['The One!', 'more or less','the bad']
# result =[]

# def get_combo_choice(event, cmb):
#     result.append(cmb.get())
#     print(result)
result =[None, None, None]
best = []

# Here's the alternative 
def get_combo_choice(event, cmb):
    i = best.index(event.widget)
    result[i] = cmb.get()
    print('result-->', result)    
    print('i-->', i)
    print('event.widget-->', event.widget)
    print('cmb.get-->', cmb.get())
    print('result[i]-->', result[i])
          
          
          
for index, heroe in enumerate(my_heroes):
    var = tk.StringVar()
    bestPlayers = ttk.Combobox(root,values=position, textvariable=var, state="readonly")
    best.append(bestPlayers)
    bestPlayers.grid(row=0 + index, column=1,padx=(15,25))
    
    label = tk.Label(root, text = heroe)
    label.grid(row=0 + index, column=0,padx=(15,25))

    bestPlayers.bind("<<ComboboxSelected>>",lambda event, cmb=var: get_combo_choice(event, cmb))

    
    button = tk.Button(root, text ="get list of choices", command = get_combo_choice)
    button.grid(row=4, column=0,padx=(15,25))

root.mainloop()

And the error is:

Exception in Tkinter callback
Traceback (most recent call last):
  File "c:pythonpython38libtkinter__init__.py", line 1892, in __call__
    return self.func(*args)
TypeError: get_combo_choice() missing 2 required positional arguments: 'event' and 'cmb'

Source: Python Questions

LEAVE A COMMENT