我有一个属性列表,如成本、价格、Isobolete等,我想动态生成一个标签,下面有一个输入字段,供用户插入正确的值。在
似乎最好的方法是根据值在字典中存储属性,并在条目字段更新时更新字典。在
我曾尝试在分配给我的输入字段的StringVar上使用trace_variable函数,但当我在字段中键入(作为测试)或在类上运行函数以获取字典的键和值时,似乎没有:
from Tkinter import *
import ttk
t = ['cost','price','isObsolete']
root = Tk()
rowAcc = 1
colAcc = 0
d = {}
for item in t:
newValue = StringVar()
def callback(*args):
print args[0] + " variable changed to " + newValue.get()
d[item] = newValue.get()
newValue.trace_variable("w", callback)
ttk.Label(root, text=item).grid(column=colAcc, row=rowAcc, sticky=(N, W))
reqEntry = ttk.Entry(root, textvariable=newValue, width=19)
reqEntry.grid(column=colAcc, row=rowAcc + 1, sticky=(N, W), pady=3, padx=1)
colAcc += 1
def printDict():
for key in d:
print key, d[key]
root.mainloop()
有没有一种方法可以调整它来实现我的结果,或者有更好的解决方案来获得动态生成的输入字段的值?在
将
StringVar
保存在数组中相关问题 更多 >
编程相关推荐