我想使用class/dict结构为数据存储创建一个模板,该结构允许我生成X值的字典,然后在tkinter窗口中编辑这些值,该窗口设置为生成X条目小部件。你知道吗
到目前为止,我已经能够创建窗口并生成输入框、标签和按钮的开头,以及使用附加代码进行值检查的尝试。我当前的问题是找到一些方法,将条目小部件拆分为具有单独名称的单独实体。我当前的尝试是将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”的迭代。你知道吗
用于验证的绑定的另一个替代方法是validatecommand特性。它可以解决用户输入时的验证问题。http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/entry-validation.html
如果您使用Entry小部件的'name'属性,那么输出数据可以构造为输入标记的字典,这可能与您作为dictionary对象传递数据的策略相匹配。你知道吗
相关问题 更多 >
编程相关推荐