简体   繁体   English

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

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

I have created a simple loop within a combobox which creates a dynamic number of entries - this in fact depends on the number of filters the user wants.我在组合框中创建了一个简单的循环,它创建了动态数量的条目 - 这实际上取决于用户想要的过滤器数量。 Ideally I'd like to store all of the user-made choices through the 'Submit' Button but I can't seem to be able to pass the variables into the ' callback ' function within the class module.理想情况下,我想通过“提交”按钮存储所有用户做出的选择,但我似乎无法将变量传递到类模块中的“回调”函数中。 As a result, I am only able to store the last combobox.结果,我只能存储最后一个组合框。 I have created a ' n ' variable which would allow for easy retrieval of each combobox.我创建了一个“ n ”变量,可以轻松检索每个组合框。 Essentially I then want to be storing all those selections in the variable ' user_selections '.基本上,我想将所有这些选择存储在变量“ user_selections ”中。 Ideally this code would then be re-used as template when facing user-selection choices.理想情况下,当面临用户选择时,此代码将被重新用作模板。

For future reference I'd also like to explore whether there is the possibility of having multiple user selections within each of the comboboxes, rather than one single dropdown selection.为了将来参考,我还想探讨是否有可能在每个组合框中有多个用户选择,而不是单个下拉选择。 I am rather new to python coding so struggling to put things together.我对 python 编码很陌生,所以很难把东西放在一起。

Any help is massively appreciated!任何帮助都非常感谢! Code below:代码如下:

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()

I've edited your code below, this should work:我在下面编辑了您的代码,这应该可以工作:

(note that I've removed a few things and cleaned it a bit). (请注意,我已经删除了一些东西并稍微清理了一下)。 Basically the problem was that you were trying to reassign to n every time, whereas you needed a list container to which to append each n.基本上问题在于您每次都试图重新分配给 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)

also, if I understand your second question correctly, I think what you are looking for is a ListBox ?另外,如果我正确理解您的第二个问题,我认为您要寻找的是ListBox

As a general comment, try keeping text and code to a minimum, the shorter the question the more people are gonna read it!作为一般性评论,尽量将文本和代码保持在最低限度,问题越短,阅读它的人就越多!

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

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