以下代码创建两个按钮:一个按钮添加一个下拉菜单,您可以在其中选择不同的选项,另一个按钮打印出选项列表。我试图用OptionMenu中的命令methon来做到这一点,并且这种方法行得通。问题是,如果您在任何下拉菜单中更改选择,它只会添加新选择,而保留先前的选择。因此,即使您仅添加一个测试,也可以获得无限列表。有没有办法创建一个函数来检索按下第二个按钮时选择的测试?
import tkinter as tk
def addtkvar(tkvar):
asdf.append(tkvar)
print(asdf)
def addBox():
next_row = len(all_entries)
tkvar = tk.StringVar(root)
choices = {'Peabody', 'Matrices de Raven', 'Teach atención selectiva', 'Caras6', 'RAN/RAS', 'SBM repetición', 'TALE II'}
tkvar.set('Select a test')
popupMenu = tk.OptionMenu(mainframe, tkvar, *choices,command=addtkvar)
popupMenu.grid(row = next_row, column = 0)
all_entries.append(1)
def check():
print(asdf)
root = tk.Tk()
mainframe = tk.Frame(root)
mainframe.pack()
asdf = []
tkvar = tk.StringVar(root)
choices = {'Peabody', 'Matrices de Raven', 'Teach atención selectiva', 'Caras6', 'RAN/RAS', 'SBM repetición', 'TALE II'}
all_entries = [1, 2, 3, 4, 5, 6]
addboxButton = tk.Button(mainframe, text='Add test', command=addBox)
addboxButton.grid(row=28, column = 0)
addboxButton2 = tk.Button(mainframe, text='Check', command=check)
addboxButton2.grid(row=29, column = 0)
root.mainloop()
谢谢!