简体   繁体   中英

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. Essentially I then want to be storing all those selections in the variable ' 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.

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.

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 ?

As a general comment, try keeping text and code to a minimum, the shorter the question the more people are gonna read it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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