Home > Blockchain >  Python 3.8. Why I'm missing 2 required positional arguments: 'event' and 'cmb�
Python 3.8. Why I'm missing 2 required positional arguments: 'event' and 'cmb�

Time:11-29

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:\python\python38\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
TypeError: get_combo_choice() missing 2 required positional arguments: 'event' and 'cmb'

CodePudding user response:

[UPD]

You can use the following:

button = tk.Button(root, text ="get list of choices")
button.grid(row=4, column=0,padx=(15,25))
button.bind("<Button-1>", lambda event, cmd=var: get_combo_choice(event, var))

Since your function get_combo_choice(event, cmd) has two positional arguments, you need to feed them as while calling the function:

This is raising different error which is something to do with best.index

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Users/nishanth.reddy/miniconda3/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
    return self.func(*args)
  File "<ipython-input-11-e63f934a14cb>", line 43, in <lambda>
    button.bind("<Button-1>", lambda event, cmd=var: get_combo_choice(event, var))
  File "<ipython-input-11-e63f934a14cb>", line 19, in get_combo_choice
    i = best.index(event.widget)
ValueError: <tkinter.Button object .!button3> is not in list

If your function requires arguments, you can use lambda, like:

button = tk.Button(
    root, 
    text ="get list of choices", 
    command=lambda : get_combo_choice(event, cmb) # will raise NameError
)

But the main point here is that event is not passed when the button is clicked. REF: How to handle a Button click event

python def button_press_handle(callback=None):
    if callback:         
        callback() # Where exactly the method assigned to btn['command'] is being callled

If you want an event object for sure, then maybe you can create a dummy local event object and pass it while calling your function.

Creating dummy event object:

        e = tkinter.Event()
        e.serial = 12345
        e.num = '??'
        e.height = '??'
        e.keycode = '??'
        e.state = 0
        e.time = 123456789
        e.width = '??'
        e.x = '??'
        e.y = '??'
        e.char = ''
        e.keysym = '??'
        e.keysym_num = '??'
        e.type = '100' # or tk.EventType.ButtonPress
        e.widget = '??'
        e.x_root = '??'
        e.y_root = '??'
        e.delta = 0

One way to get button clicked event is to use button.bind("<Button-1>", lambda event: get_combo_choice(event, None))

CodePudding user response:

I reduce to the "minimum" size and following your orientation it is working now, "huge" thank YOU:

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']


def get_combo_choice(event, cmb):
    i = best.index(event.widget)
    result[i] = cmb.get()


def get_result(): 
    print(result)  
    
    
result =[None, None, None]
best = []
       
combobox_in_loop = ['None']*3
combobox_get = []            
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))
    bestPlayers.bind("<<ComboboxSelected>>",lambda event, cmb=var: get_combo_choice(event, cmb))
#     bestPlayers.bind("<<ComboboxSelected>>", lambda event:callbackFunc(event))
    combobox_in_loop.append(bestPlayers) # = saves names of comboboxes
    combobox_get.append(bestPlayers.get())
    

button = tk.Button(root,text ="this is wrong", command=get_combo_choice )
button.grid(row=4, column=0)

button2 = tk.Button(root, text ="correct: get list of choices", command = get_result)
button2.grid(row=5, column=0)

root.mainloop()

 
  • Related