Python输入验证,用于空格键和背台



我已经为GUI编写了一个代码,该代码使我可以在3个输入小部件中输入电话号码,并将整个电话号码返回单独的小部件中的整个电话号码。我已经设法使用输入验证来限制可以输入每个小部件的字符数,也只限于数字,但是存在一些问题。

一旦将第一个数字键入每个条目,该数字就无法用backspace键删除,也将在第一个条目后,允许Spacebar键填充每个条目中的剩余空间。如何验证使用空格键的使用(Whitespace?(,还允许BackSpace键删除入口小部件的所有内容?

这是代码:

import tkinter as tk
window=tk.Tk()
window.geometry('325x75+750+350')
window.resizable(width=False,height=False)
window.title('Phone Number Test')
var=tk.StringVar(window,'')
def entrycheckone(inp):
    try:
        int(inp)
        if len(inp) == 3:
            entry2.focus_set()
            return True
        elif len(inp)<3:
            return True
        else:
            return False
        return True
    except:
        return False
def entrychecktwo(inp):
    try:
        int(inp)
        if len(inp) == 3:
            entry3.focus_set()
            return True
        elif len(inp) < 3:
            return True
        else:
            return False
        return True
    except:
        return False
def entrycheckthree(inp):
    try:
        int(inp)
        if len(inp) == 4:
            button1.focus_set()
            return True
        elif len(inp) < 4:
            return True
        else:
            return False
        return True
    except:
        return False

label1=tk.Label(text='Enter Phone Number(XXX-XXX-XXXX):', font='arial 10 bold')
label1.place(anchor='nw', x=38, y=3)
entry1=tk.Entry(width=3, font='arial 10 bold')
entry1.place(anchor='nw', x=3, y=30)
entry1.focus_set()
max1 = window.register(entrycheckone)
entry1.config(validate='key', validatecommand=(max1,'%P'))
label2=tk.Label(text='-', font='arial 20')
label2.place(anchor='nw', x=30, y=19)
entry2=tk.Entry(width=3, font='arial 10 bold')
entry2.place(anchor='nw', x=47, y=30)
max2 = window.register(entrychecktwo)
entry2.config(validate='key', validatecommand=(max2,'%P'))
label3=tk.Label(text='-', font='arial 20')
label3.place(anchor='nw', x=73, y=19)
entry3=tk.Entry(width=4, font='arial 10 bold')
entry3.place(anchor='nw', x=90, y=30)
max3 = window.register(entrycheckthree)
entry3.config(validate='key',validatecommand=(max3, '%P'))
display1=tk.Entry(width=13, textvariable=var, font='arial 10 bold',state='disabled')
display1.place(anchor='nw', x=200, y=30)
def setphone():
    num1 = entry1.get()
    num2 = entry2.get()
    num3 = entry3.get()
    wholenum = ('(' + num1 + ')-' + num2 + '-' + num3)
    print(wholenum)
    var.set(wholenum)
button1=tk.Button(text='ENTER', font='arial 10 bold', command=setphone)
button1.place(anchor='nw', x=130, y=26)
window.mainloop()

另外,是否可以使用类更轻松地完成所有三个验证?我尝试了,但失败了。

谢谢。

Mike

您应该做的第一件事是将三个近乎相同的entrycheck函数转换为一个函数。他们都做完全相同的事情,只有不同的值。明显的解决方案是使这些值函数参数。

def entrycheckinp, length, focus_widget):
    try:
        int(inp)
        if len(inp) == length:
            focus_widget.focus_set()
            return True
        elif len(inp) < length:
            return True
        else:
            return False
        return True
    except:
        return False

问题是TKINTER只会将单个参数传递给此功能。我们需要找到一种方法来包含其他2个参数的值,然后才能调用此功能。functools.partial很容易做到。使用此功能,我们可以重写

max1 = window.register(entrycheckone)
max2 = window.register(entrychecktwo)
max3 = window.register(entrycheckthree)

to

from functools import partial
max1 = window.register(partial(entrycheck, length=3, focus_widget=entry2))
max2 = window.register(partial(entrycheck, length=3, focus_widget=entry3))
max3 = window.register(partial(entrycheck, length=4, focus_widget=button1))

(您必须小心地创建所有小部件entry2entry3button1,然后再进行此操作,否则您将获得名称。(


以此为止,我们现在可以重写该功能以接受空字符串并拒绝空格。

def entrycheck(inp, length, focus_widget):
    if ' ' in inp:  # don't accept spaces
        return False
    try:
        int(inp)
        if len(inp) == length:
            focus_widget.focus_set()
            return True
        elif len(inp)<length:
            return True
        else:
            return False
        return True
    except:
        return inp == ''  # accept non-numeric input if it's the empty string

最新更新