【发布时间】:2018-06-22 01:32:08
【问题描述】:
我想使用类/字典结构创建一个用于数据存储的模板,该结构将允许我生成 X 值的字典,然后在设置为生成 X 条目小部件的 tkinter 窗口中编辑值。
到目前为止,我已经能够创建窗口并生成输入框、标签和按钮的开头,并尝试使用附加代码检查值。我当前的问题是找到某种方法将 Entry 小部件拆分为具有单独名称的单独实体。我目前的尝试是尝试将 testDict 中的键与条目值一起提供给 storeDict。
from tkinter import *
data = Tk()
# This sequence controls the label names and by extension the number of
#widgets in the frame because each one is iterated through the for loop
test_seq = ['Sample1','Sample2','Sample3','Sample4','Sample5',
'Sample6','Sample7','Sample8','Sample9','Sample10','Sample11',]
class Generator():
def __init__(self, master, sequence,r,c): #r and c for row and column
#Take a sequence and turn it into a dictionary with only keys
testDict = dict.fromkeys(sequence,'' )
#Set up the save button in the frame
save_button = Button(master, text='Save Values',command=savevalues)
save_button.grid(row=r, column=c)
for key in testDict:
r=r+1
c=c
label = Label(master,text = key + ' :')
label.grid(row=r, column=c)
entry = Entry(master)
entry.bind("<Return>",checknumber("<Return>",entry))
entry.grid(row =r,column =c+1)
# storeDict = dict(key,entry.get()) #AN attempt at making a new
#dict to store the entry info
# Limits the size of the frame to 11 rows including 0
if r==10:
r=0
c=c+2
#Beginning of button setup
def savevalues():
print('Hello, World!')
#Setting up the widgets to only accept float values. Otherwise return ERROR
def checknumber(event,entry):
print('Hello, again!')
try:
float(entry.get())
except ValueError:
entry.configure(text= 'ERROR')
testObj = Generator(data, test_seq, 0, 0)
data.mainloop()
当前状态的代码在运行时确实显示了问题,因为我尝试设置值检查时所有小部件都显示相同的文本,因为它们都是 for 循环中变量“entry”的迭代.
【问题讨论】:
标签: python python-3.x tkinter