文本输入验证

时间:2015-12-09 19:19:44

标签: python validation tkinter tkinter-entry

我正在尝试创建一个验证函数,其条件是数字大于零。我可以在python中完成它,但现在我试图在tkinter中使用输入框似乎更难。我已经包含了迄今为止的代码。

from tkinter import *


class quote():  
def __init__(self, master):

    self.master=master

    self.master.title("Quote Screen")
    self.master.geometry("2100x1400")

    self.master.configure(background = "white")
    self.Borras = PhotoImage(file = "Borras.Logo.2.gif") #sets up image
    self.Borras.image = self.Borras
    self.BorrasLabel = Label(self.master, image = self.Borras, bg = "white")#puts image onto label
    self.BorrasLabel.place(anchor=NW)

    self.Title = Label(self.master, text = "New Quote", font = ("calibri", 20), bg = "White")
    self.Title.place(x=650, y = 10)

    self.SubmitButton = PhotoImage(file = "Submit.Button.gif") #sets up image
    self.SubmitButton.image = self.SubmitButton
    self.SubmitButtonLabel = Button(self.master, image = self.SubmitButton, bg = "white", command= self.Validation)#puts image onto a button
    self.SubmitButtonLabel.place(x=900, y=290)

    global PostCodeValue
    global HeightValue
    global LengthValue
    global PitchValue
    global RiseValue

    PostCodeVar = StringVar() 
    PostCodeEntry = Entry(master,width=50, font=20, textvariable=PostCodeVar)
    PostCodeEntry.place(x = 20, y = 150)
    PostCodeVar.set("Please enter the Post Code")
    PostCodeValue = PostCodeVar.get()

    HeightVar = StringVar() 
    HeightEntry = Entry(master, width=50, font=20, textvariable=HeightVar)
    HeightEntry.place(x = 20, y = 220)
    HeightVar.set("Please enter the Height") 
    HeightValue = HeightVar.get() 

    LengthVar = StringVar()
    LengthEntry = int(Entry(master, width=50, font=20, textvariable=LengthVar))
    LengthEntry.place(x = 20, y = 290)
    LengthVar.set("Please enter the Length")
    LengthValue = LengthVar.get() 

    PitchVar = StringVar() 
    PitchEntry = Entry(master, width=50, font=20, textvariable=PitchVar)
    PitchEntry.place(x = 20, y = 360)
    PitchVar.set("Please enter the Pitch") 
    PitchValue = PitchVar.get() 

    RiseVar = StringVar() 
    RiseEntry = Entry(master, width=50, font=20, textvariable=RiseVar)
    RiseEntry.place(x = 20, y = 430)
    RiseVar.set("Please enter the Rise") 
    RiseValue = RiseVar.get()



def Validation(self):
    if LengthValue > 0 and LengthValue.isdigit():
        print('well done')
    else:
        print('Error')

2 个答案:

答案 0 :(得分:0)

您只需尝试将字符串转换为数字即可检查字符串是否为数字:

try:
    pitch = int(PitchVar.get())
    print("it is an integer")
except ValueError:
    print("it is NOT an integer")

转换后,您只需将其与您感兴趣的数字范围进行比较即可:

try:
    pitch = int(PitchVar.get())
    if pitch < 0 or pitch > 360:
        print("Not between 0 and 360")
except ValueError:
    print("it is NOT an integer")

注意:您不需要使用这些特殊变量(PitchVarPostCodevar等)。您可以通过调用窗口小部件本身的get方法从条目窗口小部件中获取值,并使用set()将值放入条目窗口小部件中:

PitchEntry = Entry(...)
PitchEntry.insert(0, "Please enter the Pitch")

pitch = int(PitchEntry.get())

答案 1 :(得分:0)

如果你不想使用try / except,你可以利用python如何处理if语句并使用类似的东西。

def Validation(self):
    if LengthVar.get().isdigit() and 0 < int(LengthVar.get()) < 360:
        print('Well Done')
    else:
        print('Error')

这里python首先检查LengthValue是否为数字。如果是,则继续下一个条件。如果为false,它将停在那里并转移到其他地方。

Bryan建议的try / except方法的另一个选项是你可以断言该值是否在0到360之间。这样你可以在这种情况下做其他事情。

def Validation(self):
    try:
        pitch = int(PitchVar.get())
        assert 0 < pitch < 360 # if value is not between 0 and 360 AssertionError is raised
    except ValueError:
        print("it is NOT an integer")
    except AssertionError:
        print("Not between 0 and 360")

附注

通过在__init__函数中进行此分配,LengthValue始终是初始值。

LengthValue = LengthVar.get()

在你的情况下,LengthVar.set("Please enter the Length")。 您应该在需要使用它时始终获得该值,因此它是当前值。

说完这个。需要分配变量的唯一窗口小部件是OptionMenu窗口小部件。您可以使用Widget.get()将所有其他值作为字符串获取。看起来像LengthEntry.get()