Search code examples
pythonvalidationtkintertkinter-entry

Using validation on text entry box


I am trying to set up validation on text entry boxes. Three of the boxes need to only accept integers and one text as a postcode. I am not sure whether to do this in a function previously defined or when the entry boxes are created. Also how would i make the values from the text entry boxes be accessable in the function QuoteCreation. All my code is below.

     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.QuoteCreation)#puts image onto a button
    self.SubmitButtonLabel.place(x=900, y=290)




    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 = 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() 

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




def on_button(self):
    print(self.entry.get())

def QuoteCreation(self):
    print(' ')

def quitWindow(self):
    self.master.destroy()  

def backToWelcome(self):
    self.master.destroy() 

Solution

  • You would set up separate functions to deal with the validation, when the submit button is pressed.

    So, as an example, your submit button may look a bit like this:

    submitButton = Button(master, text="Submit", command=validation)
    

    The validation, in your case would then want to carry out these checks:

    def validation():
        postcode = PostCodeVar.get()
        length = LengthVar.get()
        pitch = PitchVar.get()
        rise = RiseVar.get()
    
        if postcodeCheck(postcode) == True and length.isdigit() == True and pitch.isdigit() == True and rise.isdigit() == True:
            #carry out chosen process
    

    In your case, you can try setting the postcode, length, pitch and height variables before calling the function, and setting them as global. The postcode should be created, and if it is okay, the function should then:

    return True
    

    ...so it matches the outcome of the if statement.

    I hope this is what you were looking for, and can adapt the example to your specific problem!