Giuseppe Marziano
Giuseppe Marziano

Reputation: 69

How to assign different value to dynamic listboxes?

Here is the code in my class that will generate new Listboxes and allow them to be controlled simultaneously

for i in range(5):
            self.lb = tk.Listbox(self.modeSelect)
            self.lb.configure(background='#2f2a2d', exportselection='false', font='{Arial} 12 {}', foreground='#feffff', height='23')
            self.lb.configure(relief='flat', width='12')
            self.lb.pack(side='left')
            for j in range(10):
                self.lb.insert("end", f"{i+1},{j+1}")
            self.lb.bind("<Double-1>", self.getIndexLB)

I can insert or delete the same value across all 5 listboxes using this function

    def addSeq(self, event=None):
        yUser = yInput.get().strip()
        xUser = xInput.get().strip()
        repeatInt = repeatInpu.get().strip()
##      interval_Input = iInput.get().strip()
        clickAmount = int(repeatInt)
        for child in self.modeSelect.winfo_children():
            if isinstance(child, tk.Listbox):
                print(child)
                child.insert(END, yUser)
##                if child.get('end') == yUser:
##                    child.insert(END, 'test')

My question is, if I want to input different values in each of the 5 listboxes, how can I do that?

Here is the result of print(child)

.!labelframe.!listbox
.!labelframe.!listbox2
.!labelframe.!listbox3
.!labelframe.!listbox4
.!labelframe.!listbox5

I've tried to use ....!labelframe.!listbox.insert(END, 'value') but it keeps printing the initial child value across all 5, I've also tried to use an if statement to try and continue if the initial child has the same value as my the variable I want to put in, but had no luck.

Any help would be much appreciated!

Upvotes: 0

Views: 35

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385900

My question is, if I want to input different values in each of the 5 listboxes, how can I do that?

The simplest solution would be to save the listboxes to a list or dictionary so that you can directly reference them.

self.listboxes = []
for i in range(5):
    lb = tk.Listbox(self.modeSelect)
    self.listboxes.append(lb)
    ...
...
self.listboxes[0].insert("end", "this is a new value for listbox 0")
self.listboxes[1].insert("end", "hello, listbox 1")

Upvotes: 1

Related Questions