【问题标题】:Validation of Data inputed with Tkinter [closed]使用 Tkinter 验证数据输入
【发布时间】: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()

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    关键词

    tkinter 条目验证

    在 ddg 或 google 中引导您到

    文档内容:

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

    正是您正在寻找的,所以我想您没有花任何时间进行研究。

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

    【讨论】:

    • 这对我来说似乎是一个“仅限链接”的答案。你能总结一下这些链接的内容吗?也许这个 OP 没有做太多研究,但是惩罚发生在这个帖子上的未来研究人员似乎是不公平的 :)
    • 这样更好? ;)
    • 你看起来好多了。我点击了 effbot 链接,上面写着:“较新版本的 Tkinter 支持 validate 和 validatecommand 选项,它们可以用来代替额外的变量。”这似乎是相关的 - 这与添加你描述的回调一样吗?
    • 我问是因为那个 effbot 链接是 1998 年的,前几天我因为发布了一个 1998 年的 effbot 链接而遇到了“麻烦”,哈哈
    • 添加了有关该主题的其他信息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    相关资源
    最近更新 更多