tkinter .insert adds lines to two listboxes at once

Question:

I’m creating a tkinter GUI and I have two list boxes that are supposed to have the same single line of text inserted into them upon initialization. The listboxes have separate names, but each time I run .insert for either of the listboxes, both of them get the line added to them and I can’t figure out why. The same thing happens when they get their unique data. Here is example code copied from my program:

names = ["+ Add New Condition"]
bcList_lsb = tk.Listbox(bc_frm,listvariable=names)
bcList_lsb.grid(row = 1, column =1)
bcList_lsb.bind('<<ListboxSelect>>',command = bcLbClick)
bcList_lsb.insert("end",names[0])

solNames = ["+ Add New Condition"]
solBcList_lsb = tk.Listbox(solBc_frm,listvariable = solNames)
solBcList_lsb.grid(row = 1, column =1)
solBcList_lsb.bind('<<ListboxSelect>>',command = solBcLbClick)
solBcList_lsb.insert("end",solNames[0])

The setup for the two frames is identical except for their names and their commands.
Here is what it looks like in the GUI upon initialization:

enter image description here

enter image description here

Any suggestions would be appreciated.

Asked By: Carson M

||

Answers:

You have in effect given both listboxes the same listvariable which forces them to be in sync. When you specify the listvariable option, the string representation of what you give it becomes an internal variable name. Both widgets end up with a variable named "['+ Add New Condition']" (eg: str(["+ Add New Condition"])) which means they are the same variable internally.

Answered By: Bryan Oakley
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.