【发布时间】:2019-08-01 08:53:57
【问题描述】:
我正在根据用户输入的整数设置多个输入字段和跟踪变量。然后将条目信息存储到字典中的列表中。 存在两个按钮:“启用”,它启用输入字段以进行编辑,但同时从字典中获取信息并将其显示在输入字段上。 “禁用”按钮清除输入字段并禁用编辑。还更新字典。 问题是将每个单独的条目读回到条目字段中,字典如下所示:
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("")
disabled 选项执行其确切功能:初始化“F”,重新读取条目,将其存储在 dict_test 中并禁用条目。 但是,“启用”在尝试循环“F”时显示“索引超出范围”错误
【问题讨论】:
-
设法解决了我自己的问题,对于任何感兴趣的人:循环 'F' 需要它的 OWN 索引,它也用作 sv 的索引:''' for j in range(len(dict_test ['A'][3]['F'])): sv[j].set(dict_test['A'][3]['F'][j]) # ´´´
-
将您的解决方案作为答案并将其标记为已接受。
标签: python list dictionary tkinter tkinter-entry