【发布时间】:2020-08-10 12:48:58
【问题描述】:
我想循环显示一个下拉列表并捕获所选数据。我有下面的代码,但即使打印它也不起作用..有人可以帮我吗?
假设在第一个屏幕上我按顺序选择了 a、b、c,然后在第二个屏幕上按顺序选择了 d、c、a;最后我按顺序选择了 a、d、c,我希望我的最终列表是:
lb_sel = [['a', 'b', 'c'],['d', 'c', 'a'],['a', 'd', 'c']]
from tkinter import filedialog, messagebox, ttk, constants
from tkinter import *
def DropDown(argslist,index):
var = StringVar()
c= ttk.Combobox(root,width=40)
c['values'] = argslist
c.textvariable=var
c.current(0)
c.bind("<<ComboboxSelected>>",choice_sel)
c.grid(row=index-1, column=1,padx=(15,25))
c.index=index
nb_choice = 3
has_choice = True
lst = ['a', 'b', 'c', 'd']
lb_sel = [[] for i in range(nb_choice)]
lst.insert(0,'Select a choice')
for i in range(nb_choice):
root = Tk()
root.focus_force()
root.title('Choices')
labl1 = ttk.Label(text = 'Select the first choice of N.' + str(i+1))
labl1.grid(row = 0, column = 0)
labl2 = ttk.Label(text = 'Select the second choice of N.' + str(i+1))
labl2.grid(row = 1, column = 0)
labl3 = ttk.Label(text = 'Select the third choice of N.' + str(i+1))
labl3.grid(row = 2, column = 0)
def choice_sel():
print(lb1.get())
lb1 = DropDown(lst,1)
lb2 = DropDown(lst,2)
lb3 = DropDown(lst,3)
exit_but = Button(root, text = 'OK', command = lambda: choice_sel, height = 3, width = 5)
exit_but.grid(row = 2, column = 2)
root.mainloop()
【问题讨论】:
标签: python-3.x loops tkinter combobox dropdown