简体   繁体   English

TKinter:迭代时条目未使用正确的文本变量

[英]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文本变量外部存储在字典 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:如果我有 3 组,我正在寻找的结果是:按下 set 0 的 Edit 按钮:

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!原来 [{}]*listlength 不适用于启动字典列表,问题解决了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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