【发布时间】:2016-03-19 10:47:26
【问题描述】:
我正在为一个计算项目开发一个潜水日志软件。我需要一种方法来验证用户通过Tkinter 输入的数据。我认为最好的方法是在之后验证数据,然后,如果它不正确,就会有一个错误消息窗口,让用户重新输入数据。
有没有人知道这样做的方法?
这是我用于数据输入的代码的 pastebin 的链接:这是我迄今为止尝试过的:
E1_data = None #These four lines initialise the variables for data input using Tkinter
E2_data = None
E3_data = None
E4_data = None
def tkinput():
root = Tk()
label1 = Label(root, text="Dive Number: ")#label for the first data entry
E1 = Entry(root, bd = 5)
label2 = Label(root, text="Time In (24hr clock): ")#label for the second data entry
E2 = Entry(root, bd = 5)
label3 = Label(root, text="Time Out (24hr clock): ")#label for the third data entry
E3 = Entry(root, bd = 5)
label4 = Label(root, text="Max Depth: ")#label for the fourth data entry
E4 = Entry(root, bd = 5)
def get_data(): #Subroutine for getting the data and assigning it to a variable
global E1_data
E1_data = E1.get()
global E2_data
E2_data = E2.get()
global E3_data
E3_data = E3.get()
global E4_data
E4_data = E4.get()
root.destroy()
#these next lines render all of the required items onto the input window
label1.pack()
E1.pack()
label2.pack()
E2.pack()
label3.pack()
E3.pack()
label4.pack()
E4.pack()
submit = Button(root, text = "Submit", command = get_data) #creates the submit button to be displayed on the input window
submit.pack(side = BOTTOM) #displays the button at the bottom of the input window
root.mainloop() #runs the input module
tkinput()
【问题讨论】: