简体   繁体   中英

TKinter: Entry not using correct textvariable when iterating

I have a loop that constructs multiple sets of Entries with Buttons next to them so:

set 0: [Item0 Entry] [Item0 Edit Button]
.
.
.
set 1: [Item0 Entry] [Item0 Edit Button]
etc

During each iteration I create a variable

Item0Var = StringVar()

Which I then initialise through say

Item0Var.set("None")

I store the variable in an external dictionary

self.CurrentEquipSets[set_num].update(
            {"Item0" : Item0Var,...} )

The variable is then displayed in a disabled Entry

Item0Entry = Entry(self.Item0Frame,textvariable=self.CurrentEquipSets[set_num]["Item0"],state=DISABLED,width=EntryWidth)

The button is

Item0Edit = Button(self.Item0Frame,text="Edit",command = lambda EquipSet=set_num,Item="Item0": self.EditItem(EquipSet,Item))

The textvariable is stored externally in the dictionary self.CurrentEquipSets

The button calls

def EditItem(self,EquipSet,Item):
    self.CurrentEquipSets[EquipSet][Item].set("Something")

The result I am looking for, if I have 3 sets, from pressing the Edit button for set 0 is:

set 0: Item0Entry ["Something"]
set 1: Item0Entry ["None"] 
set 3: Item0Entry ["None"]

But instead I get

set 0: Item0Entry ["None"]
set 1: Item0Entry ["None"] 
set 3: Item0Entry ["Something"]

What am I missing?

Turns out [{}]*listlength does not work for initating a list of dictionaries, the problem is solved!

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