擅长:python、mysql、java
<p>由于对<code>StringVar</code>使用相同的变量<code>varCombo</code>,因此只有<code>StringVar</code>的最后一个实例引用了它,其他实例将被垃圾收集</p>
<p>如果您想稍后访问这些<code>StringVar</code>,最好使用字典来存储它们:</p>
<pre><code>varCombo = {}
counter = 0
for i in infos:
frame = Frame(principalFrame, bd=1)
frame.grid(row=counter, column=0, pady=20)
frame.columnconfigure(0,weight=1)
label = Label(frame, text=i.name)
label.grid(row=0, column=0, sticky="news")
label.columnconfigure(0,weight=1)
var1 = StringVar(window)
var1.set(i.default)
combo = ttk.Combobox(frame, state="readonly", textvariable=var1, values=i.values)
combo.grid(row=1, column=0, sticky="news")
combo.columnconfigure(0, weight=1)
combo.rowconfigure(0, weight=1)
varCombo[i.name] = var1
counter = counter + 1
</code></pre>