使用Tkinter验证数据

时间:2016-03-19 10:47:26

标签: python tkinter

我正在为计算项目开发一个潜水日志软件。我需要一种验证用户通过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()

1 个答案:

答案 0 :(得分:0)

关键字

tkinter条目验证

在ddg或谷歌指导你

文件内容如下:

您可以像条目回调一样向条目小部件本身添加验证命令/功能/方法。

完全你在寻找什么,所以我想你没有花时间研究。

是的,Effbot Link是从1998年开始的,但代码仍然有效。然而,更好的方法可能是直接在Tkinter中实现的验证命令。

相关问题