Grace
Grace

Reputation: 43

How to validate entry in tkinter?

I have found some answers to this question but none of the answers have worked on my program. I am trying to validate the name variable in my code but isalpha() is not a function used in tkinter.

Here is my code so far:

class newUser:
    root.title("Sign Up")
    header = Label(root, text = "Sign Up!\n")
    header.grid(row = 0, column = 0, sticky = E)

    global results
    results = False

    def getName(): #Getting name of the user
        global name

        nameX = Label(root, text = "Please enter your name: ")
        nameX.grid(row = 1, column = 0, sticky = W)

        name = Entry(root)
        name.grid(row =1, column = 1)
        #name = name.get()

        if name.isalpha() and name != "":
            name = name
            #newUser.getAge()
        else:
            print("Please ensure you have entered your name. Thank you. ")
            newUser.getName()
        root.mainloop()


newUser.getName()

I tried to use the name.get() but it created an endless loop. Any ideas?

Upvotes: 1

Views: 2149

Answers (1)

Erick Shepherd
Erick Shepherd

Reputation: 1443

isalpha() is a built-in function of str objects. Calling isalpha() on name, which was defined to be a tkinter.Entry object will raise an exception because tkinter.Entry has no such function. While you are correct in attempting to use the get() function, which is a function of tkinter.Entry objects and does return a str which supports isalpha() calls, your implementation is a bit off.

You are entering into a recursion because name.get() immediately returns a string which fails the conditional on the name != "" check causing the Python interpreter to fall into the catch-all else clause which calls newUser.getName(), the function we were already in, and the process repeats until you exceed Python's maximum recursion depth. You don't want to call get() on the tkinter.Entry object immediately because that gives the user no time to enter anything. Instead, get() should be called after some event has occurred, such as a submit button being pressed.

Instead, try something like the following:

import tkinter as tk

root = tk.Tk()

class NewUser:

    def __init__(self):

        self.name = None

        root.title("Sign Up")

        self.headerLabel  = tk.Label (root, text = "Sign Up!\n")
        self.nameLabel    = tk.Label (root, text = "Please enter your name: ")
        self.nameField    = tk.Entry (root)
        self.submitButton = tk.Button(root, text = "Submit", command = self.saveName)

        self.headerLabel.grid (row = 0, column = 0, columnspan = 2)
        self.nameLabel.grid   (row = 1, column = 0, sticky = "W")
        self.nameField.grid   (row = 1, column = 1)
        self.submitButton.grid(row = 2, column = 0, columnspan = 2, sticky = "EW")

    def saveName(self):

        name = self.nameField.get()

        if name.isalpha() and name != "":

            self.name = name
            print("Name saved: {}".format(self.name))

        else:

            print("Please ensure you have entered your name. Thank you.")

user = NewUser()

root.mainloop()

This code will generate the following window:

tkinter window without entry text

At this point, if you hit the Submit button, name will be an empty string and will fail the name != "" comparison. Consequently, the following is printed to the console:

Please ensure you have entered your name. Thank you.

However, if you enter your name, assuming you only include alpha characters like so:

tkinter window with entry text

The following is printed to the console upon pressing Submit:

Name saved: Erick

And the saved name is now accessible via the self.name member variable of our NewUser class.

Upvotes: 3

Related Questions