繁体   English   中英

获取组合框中所有用户选择的值

[英]Getting all user-selected values within the Combobox

我在组合框中创建了一个简单的循环,它创建了动态数量的条目 - 这实际上取决于用户想要的过滤器数量。 理想情况下,我想通过“提交”按钮存储所有用户做出的选择,但我似乎无法将变量传递到类模块中的“回调”函数中。 结果,我只能存储最后一个组合框。 我创建了一个“ n ”变量,可以轻松检索每个组合框。 基本上,我想将所有这些选择存储在变量“ user_selections ”中。 理想情况下,当面临用户选择时,此代码将被重新用作模板。

为了将来参考,我还想探讨是否有可能在每个组合框中有多个用户选择,而不是单个下拉选择。 我对 python 编码很陌生,所以很难把东西放在一起。

任何帮助都非常感谢! 代码如下:

import tkinter as tk
from tkinter import *
from tkinter import ttk

class User_ComboBox(Tk):
    def __init__(self, s, options):

        Tk.__init__(self)
        self.title("User Selections: ")
        x = s*100
        y = s*50
        self.geometry(str(x) + "x" + str(y) + '+350+350')
        
        self.labelTop = tk.Label(self,text = "Data Slicing Options: ")
        self.labelTop.grid(row = 0, column = 0, sticky = W, pady = 2)
        
        for i in range(1, s+1):
            n = "combobox_" + str(i)
            self.label = Label(self,text="Select Criteria " + str(i))
            self.label.grid(row = i, column = 0, sticky = W, pady = 2)
            self.n = ttk.Combobox(self,values=options[i - 1])
            self.n.grid(row = i, column = 1, sticky = W, pady = 2)
        
        self.okButton = tk.Button(self, text='Submit',command = self.callback)
        self.okButton.grid(row = i + 1, column = 0, sticky = W, pady = 2)
        
    def callback(self):
        """ Get the contents of the Entry and exit """
        self.comboBox_contents = {'a':self.n.get()}
        self.destroy()

def ComboboxSelection():
    options = [['Layer 1','Layer 2','Layer 3'],['Americas','APAC','EMEA'],['Bank','Institution','Fund']]
    n_comboboxes = 3
    selection = User_ComboBox(n_comboboxes, options)
    selection.mainloop()
    
    return selection.comboBox_contents

user_selections = ComboboxSelection()

我在下面编辑了您的代码,这应该可以工作:

(请注意,我已经删除了一些东西并稍微清理了一下)。 基本上问题在于您每次都试图重新分配给 n,而您需要一个列表容器来附加每个 n。

from tkinter import *
from tkinter import ttk

class User_ComboBox(Tk):
    def __init__(self, s, options):

        Tk.__init__(self)
        self.title("User Selections: ")
        self.comboBox_contents = []
        self.comboBoxes = [[] for n in range(s)]
        x = (s+1)*100
        y = (s+1)*50
        self.geometry(str(x) + "x" + str(y) + '+350+350')
        
        self.labelTop = Label(self,text = "Data Slicing Options: ")
        self.labelTop.grid(row = 0, column = 0, sticky = W, pady = 2)
        
        for i in range(s):
            self.label = Label(self,text="Select Criteria " + str(i))
            self.label.grid(row = i+1, column = 0, sticky = W, pady = 2)
            self.comboBoxes[i] = ttk.Combobox(self, values = options[i])
            self.comboBoxes[i].grid(row = i+1, column = 1, sticky = W, pady = 2)
        
        self.okButton = Button(self, text='Submit',command = self.callback)
        self.okButton.grid(row = i + 2, column = 0, sticky = W, pady = 2)
        
    def callback(self):
        """ Get the contents of the Entry and exit """
        self.comboBox_contents = [x.get() for x in self.comboBoxes]
        self.destroy()

def ComboboxSelection():
    options = [['Layer 1','Layer 2','Layer 3'],['Americas','APAC','EMEA'],['Bank','Institution','Fund']]
    n_comboboxes = 3
    selection = User_ComboBox(n_comboboxes, options)
    selection.mainloop()
    
    return selection.comboBox_contents

user_selections = ComboboxSelection()
print(user_selections)

另外,如果我正确理解您的第二个问题,我认为您要寻找的是ListBox

作为一般性评论,尽量将文本和代码保持在最低限度,问题越短,阅读它的人就越多!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM