【问题标题】:Tk Python combobox in loop get the data selected循环中的 Tk Python 组合框获取选定的数据
【发布时间】: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


    【解决方案1】:

    下面的代码完成了工作:

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

    我为使其正常工作所做的更改是:

    1. 在 for 循环之外设置 choice_sel 并添加 *args*kwargs 参数,因为它用于代码的两个不同位置,其中一个是传递 event 参数所必需的。这实际上在运行时导致了错误。
    2. index 参数也传递给函数,以了解哪个是按下的下拉菜单。
    3. 通过widget.get()方法获取组合框的值。
    4. 从函数DropDown 中删除返回变量,因为它不返回任何值。

    请记住,当您关闭主窗口以进入 for 循环的下一次迭代时,您可能应该重置/保存变量 lb_sel

    附注:最好使用TopLevel 而不是创建Tk 的多个实例。

    【讨论】:

    • 嗨大卫,非常感谢您的帮助。我是 Tk 的新手,显然还有更多要学的东西!但我尝试运行它,但随后出现以下错误:Exception in Tkinter callback Traceback (most recent call last): File "C:\xx\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) TypeError: choice_sel() missing 1 required positional argument: 'index' 我无法进行下一次迭代。我尝试使用choice_sel(i) 传递它,但这也不起作用,所以我我不确定。。
    • 我没有收到这个错误,但可能是按钮exit_but下的命令引起的。其实我不认为你想为这个按钮调用函数choice_sel,对吧?你能试着改变这个命令吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    相关资源
    最近更新 更多