Skip to main content
 首页 » 编程设计

python之tkinter 条目小部件的验证

2024年11月24日8bonelee

<分区>

我的 tkinter GUI 应用程序中有一个输入字段。 entry 应该只接受包括小数点的数字。我使用了一个函数来验证条目。问题是,如果前面有数字,它不接受小数点 (.)。 (示例 25.3 不接受)。如果开头有一个点,则此后不接受任何数字。谁能帮我解决这个问题。以及将输入字段中的最大值限制为 1000 的任何建议?

import tkinter as tk 
 
def acceptNumber(inp): 
    if inp.isdigit(): 
        return True 
    elif inp is "": 
        return True 
    elif inp is ".": 
        return True 
    else: 
        return False 
win = tk.Tk() 
reg = win.register(acceptNumber) 
 
entryHere =tk.Entry(win) 
entryHere.place(x=400, y=200) 
 
entryHere.config(validate="key", validatecommand =(reg, '%P')) 
win.mainloop()