0

I'm working on a small project and I'm having issues retrieving the values stored in combo boxes. The program has a "plus" button that creates additional boxes beneath the existing ones. They are created by calling a "create" function that makes a new instance of the ComboBox class, where the box is created and put onto the screen. A separate "submit" function is then supposed to loop through and retrieve all of the box values and store them in a list. My main flaw is that I used data in the variable names, but I have no clue how else to do this in this scenario. Does anyone have an alternative solution?

(there are some off screen variables that are show used here as parameters, but there are definitely not the source of the issue)

class ComboBox:
    def __init__(self, master, counter, fields):
        self.master = master
        self.counter = counter
        self.fields = fields

        self.field_box = ttk.Combobox(width=20)
        self.field_box["values"] = fields
        self.field_box.grid(row=counter + 1, column=0, pady=5)

    def get_value(self):
        value = self.field_box.get()
        return value
def create():
    global entry_counter
    name = "loop"+str(entry_counter-1)
    name = ComboBox(window, entry_counter, fields)
    values.append(name.get_value())
    entry_counter += 1


def submit():
    for i in range(1, entry_counter):
        name = "loop" + str(entry_counter-1)
        values.append(name.get_value())

For example, if I created 2 boxes and selected the options "test1" and "test2" I would want the my values list to contain ["test1, "test2"]

Bejdok
  • 3
  • 1

2 Answers2

2

Not sure I understand the question right, but I guess you are asking about how to loop throw all instances of ComboBox. You can just create an global array, append new instance into it in create() method:

comboboxes = []
def create():
    ...
    comboboxes.append(new_instance)

def submit():
    for combobox in comboboxes:
        ...
Huy Ngo
  • 362
  • 1
  • 8
1

You're on the right track with .get(). I believe your solution is that your get_value function also needs an event parameter:

def get_value(self, event):
    value = self.field_box.get()
    return value

See the following:

Getting the selected value from combobox in Tkinter

Retrieving and using a tkinter combobox selection

zerecees
  • 697
  • 4
  • 13