當我使用組合框時,腳本列印正常。但是當我使用按鈕“獲取選擇串列”時,我收到錯誤訊息“get_combo_choice() 缺少 2 個必需的位置引數:'event' 和 'cmb'”。我想不通。感謝您的任何提示。
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()
錯誤是:
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'
uj5u.com熱心網友回復:
[更新]
您可以使用以下內容:
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))
由于您的函式get_combo_choice(event, cmd)有兩個位置引數,您需要在呼叫函式時提供它們:
這引發了不同的錯誤,這與 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
如果您的函式需要引數,您可以使用 lambda,例如:
button = tk.Button(
root,
text ="get list of choices",
command=lambda : get_combo_choice(event, cmb) # will raise NameError
)
但這里的要點是event當按鈕被點擊時不會通過。REF:如何處理按鈕單擊事件
python def button_press_handle(callback=None):
if callback:
callback() # Where exactly the method assigned to btn['command'] is being callled
如果您event確實想要一個物件,那么也許您可以創建一個虛擬的本地事件物件并在呼叫您的函式時傳遞它。
創建虛擬事件物件:
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
獲取按鈕單擊事件的一種方法是使用 button.bind("<Button-1>", lambda event: get_combo_choice(event, None))
uj5u.com熱心網友回復:
我縮小到“最小”尺寸并按照您的方向它現在正在作業,“巨大”謝謝您:
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/369274.html
