我正在根据用户输入的整数设置多个输入字段和跟踪变量。然后将条目信息存储到字典中的列表中。 存在两个按钮:“启用”,它启用用于编辑的输入字段,但同时从字典中获取信息并将其显示在输入字段上。 “禁用”按钮清除输入字段并禁用编辑。还更新字典。 问题是使用字典将每个单独的条目读回条目字段,如下所示:
dict_test = {'A': ['B', [1, 1], True, {'E': '', 'F': [], 'G': []}]}
我无法选择“f”的单个值,并在选择“启用”时将其显示在相应的入口字段中。
我知道如何访问“F”的整个列表 (dict_test['A'][3]['F'])
# User input:
phases = 3
# The Dictionary to which the trace-variables are stored
dict_test = {'A': ['B', [1, 1], True, {'E': '', 'F': [], 'G': []}]}
headings = ["FRa"]
sv = [StringVar() for i in range(phases)] # Holds the Entry inputs
fra_list = []
# The Entry-fields
fra_entries = [Entry(frame, width=10, state=DISABLED, justify=RIGHT, textvariable=sv[i]) for i in range(phases)]
...
enable_button = Button(root, text="Enable", command=lambda: enable())
disable_button = Button(root, text="Disable", command=lambda: disable())
...
def enable():
for i in range(len(fra_entries)):
fra_entries[i].config(state=NORMAL)
sv[i].set(dict_test['A'][3]['F'][i]) # Causes the problem !!!!
def disable():
dict_test['A'][3]['F'] = []
fra_list = []
for i in range(phases):
fra_list.append(sv[i].get())
dict_test['A'][3]['F'].append(fra_list[i])
for i in range(len(fra_entries)):
fra_entries[i].config(state=DISABLED)
sv[i].set("")
禁用选项执行其确切功能:初始化“F”,重新读取条目,将其存储在 dict_test 中并禁用条目。 但是,“启用”在尝试循环“F”时显示“索引越界”错误
请您参考如下方法:
for j in range(len(dict_test['A'][3]['F'])):
sv[j].set(dict_test['A'][3]['F'][j])
