如何在python tkinter中验证条目小部件

时间:2018-09-03 08:06:21

标签: python user-interface tkinter

我目前正在研究基本的计算器程序。我正在尝试使用validate函数,以便用户只能输入valild_input列表中的值。包含此列表的test_input函数可以正常工作,直到我决定键入“ =”或按equals button为止。当我按下equals_button时,display条目上的当前方程式不会被删除,而是替换为结果。虽然在按键盘上的“ =”键时不会发生这种情况。唯一的问题是等号停留在display上,之后,输入小部件将完全停止验证用户的输入。

from tkinter import *
from tkinter import messagebox

def replace_text(text):
    display.delete(0, END)
    display.insert(0, text)

#Calculates the input in the display        
def calculate(event = None):
    equation = display.get()
    try:
        result = eval(equation)
        replace_text(result)
    except: 
        messagebox.showerror("Error", "Math Error", parent = root)

def test_input(value, action):
    valid_input = ["7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "/"]
    if action == "1":
        if value not in valid_input:
            return False
        return True

root = Tk() 
root.title("Calculator testing")

display = Entry(root, font=("Helvetica", 16), justify = "right", validate = "key")
display.configure(validatecommand = (display.register(test_input), "%S", "%d"))
display.insert(0, "")
display.grid(column = 0, row = 0, columnspan = 4, sticky = "NSWE", padx = 10, pady = 10)
display.bind("=", calculate)

#Equals button
button_equal = Button(root, font = ("Helvetica", 14), text = "=", command = 
calculate, bg = "#c0ded9")
button_equal.grid(column = 2, row = 1, columnspan = 2, sticky = "WE")

#All clear button 
button_clear = Button(root, font = ("Helvetica", 14), text = "AC", command = lambda: replace_text(""), bg = "#c0ded9")
button_clear.grid(column = 0, row = 1, columnspan = 2, sticky = "WE")

#Main Program       
root.mainloop()

1 个答案:

答案 0 :(得分:3)

您的代码有2个问题。

  1. 验证功能应始终返回布尔值。

    来自this answer

      

    验证命令返回True或False很重要。任何其他情况都会导致该窗口小部件的验证被关闭。

    您的test_input函数无法执行此操作-分支中返回了None

    def test_input(value, action):
        valid_input = ["7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "/"]
        if action == "1":
            if value not in valid_input:
                return False
            return True
        # None is being returned here!
    

    这就是为什么您的程序从Entry中删除文本后禁用验证的原因。解决方法很简单:返回True而不是None

    def test_input(value, action):
        valid_input = ["7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "/"]
        if action == "1":
            if value not in valid_input:
                return False
            return True
    
        # if action != 1, allow it
        return True
    
  2. 验证功能需要处理多字符输入。

    您已经假定对输入的每个字符都调用了验证功能。当用户使用键盘输入公式时,情况就是如此,但是当复制/粘贴或使用.insert(...)设置条目文本时,情况并非如此。您的职能部门需要处理这些情况。

    def test_input(value, action):
        valid_input = ["7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "/"]
        if action == "1":
            return all(char in valid_input for char in value)
    
        # if action != 1, allow it
        return True