Tk Python 循环中的组合框获取所选数据
Tk Python combobox in loop get the data selected
我想循环显示下拉列表并捕获所选数据。我有以下代码,但即使打印它也无法正常工作。有人可以帮我吗?
所以假设在第一个屏幕上我按顺序选择了 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()
以下代码可以完成工作:
from tkinter import filedialog, messagebox, ttk, constants
from tkinter import *
def choice_sel(index, *args, **kwargs):
lb_sel[index-1].append(args[0].widget.get())
def DropDown(argslist,index):
var = StringVar()
c= ttk.Combobox(root,width=40)
c['values'] = argslist
c.textvariable=var
c.current(0)
c.bind("<<ComboboxSelected>>",lambda event, index=index: choice_sel(index, event))
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)
DropDown(lst,1)
DropDown(lst,2)
DropDown(lst,3)
exit_but = Button(root, text = 'OK', command = choice_sel, height = 3, width = 5)
exit_but.grid(row = 2, column = 2)
root.mainloop()
在第一个下拉菜单中按 'a' 和 'b' 以及在第二个下拉菜单中按 'b' 后的结果:
lb_sel = [['a', 'b'], ['b'], []]
我为使其正常工作所做的更改是:
- 在 for 循环外设置
choice_sel
并添加 *args
和 *kwargs
参数,因为它在代码的两个不同位置使用,其中之一是传递 event
参数。这实际上在 run-time. 期间导致错误
- 也将
index
参数传递给函数以了解按下的下拉菜单。
- 使用方法
widget.get()
获取组合框的值。
- 从函数
DropDown
中删除 return 变量,因为它 return 没有任何值。
请记住,您可能应该 reset/save 变量 lb_sel
当您关闭主 window 以转到 for-loop 的下一次迭代时。
旁注:最好使用 TopLevel
而不是创建 Tk
.
的多个实例
我想循环显示下拉列表并捕获所选数据。我有以下代码,但即使打印它也无法正常工作。有人可以帮我吗?
所以假设在第一个屏幕上我按顺序选择了 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()
以下代码可以完成工作:
from tkinter import filedialog, messagebox, ttk, constants
from tkinter import *
def choice_sel(index, *args, **kwargs):
lb_sel[index-1].append(args[0].widget.get())
def DropDown(argslist,index):
var = StringVar()
c= ttk.Combobox(root,width=40)
c['values'] = argslist
c.textvariable=var
c.current(0)
c.bind("<<ComboboxSelected>>",lambda event, index=index: choice_sel(index, event))
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)
DropDown(lst,1)
DropDown(lst,2)
DropDown(lst,3)
exit_but = Button(root, text = 'OK', command = choice_sel, height = 3, width = 5)
exit_but.grid(row = 2, column = 2)
root.mainloop()
在第一个下拉菜单中按 'a' 和 'b' 以及在第二个下拉菜单中按 'b' 后的结果:
lb_sel = [['a', 'b'], ['b'], []]
我为使其正常工作所做的更改是:
- 在 for 循环外设置
choice_sel
并添加*args
和*kwargs
参数,因为它在代码的两个不同位置使用,其中之一是传递event
参数。这实际上在 run-time. 期间导致错误
- 也将
index
参数传递给函数以了解按下的下拉菜单。 - 使用方法
widget.get()
获取组合框的值。 - 从函数
DropDown
中删除 return 变量,因为它 return 没有任何值。
请记住,您可能应该 reset/save 变量 lb_sel
当您关闭主 window 以转到 for-loop 的下一次迭代时。
旁注:最好使用 TopLevel
而不是创建 Tk
.