Python tkinter input validation

Only digits are allowed

Validating Digits Entry only

Tkinter validation of user entry using call back function on keypress on focus in & out using filter

validate='key', is used to check for validation when any key is used. Check the other options for validate at the end of this tutorial.
validatecommand=(my_valid,'%S'), is used to trigger the callback function validate() by sending the input and getting the status as True or False.
my_valid = my_w.register(validate) Register the callback function
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("400x100")  # Size of the window 
my_w.title("")  # Adding a title
def validate(u_input): # callback function
    return u_input.isdigit()
my_valid = my_w.register(validate) # register 
l1=tk.Label(my_w,text='Enter Number only')
e1 = Entry(my_w,validate='key',validatecommand=(my_valid,'%S'))

my_w.mainloop()  # Keep the window open

Alphnumeric only

Only numbers and alphabets are allowed, other chars will not be accepted. Here we are ony indicating the line inside the validate(u_input) function of above code.
Read more on isalnum() here.
def validate(u_input):
    #return u_input.isdigit() # use other string functions.  
    return u_input.isalnum()
By using other string functions we can extend the validation to different requirments. Here is the list.

isalnum()Check if all chars are alphanumeric in a string
isalpha()Check if all chars are alphabets in a string
isdecimal()Check if all chars are decimal numbers in a string
isdigit()Check if all chars are digits in a string
isidentifier()Check if the string is identifier
islower()Check if all chars are lower case only
isnumeric()Check if all chars are numeric
isprintable()Check if all chars are printable or not
isspace()Check if all chars are whitespace or not
istitle()* Check if all words starts with upper case letters ( use %P )
isupper()Check if all chars are upper case letters
* To use whole string use %P instead of %S. For some checking we have to consider the whole string like istitle()
Validating Title using istitle()

Validating email address

Validating Email
Validating Email disabled button
Here we will use focusout event which will validate the email format. One button is kept which will be disabled if validation is not clear and will get enabled again if correct format is used for email address.
Here regular experssion is used to check the email format.
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
import re     
regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
my_w.geometry("400x100")  # Size of the window 
my_w.title("")  # Adding a title
def validate(u_input):
    if(,u_input) and u_input.isalpha):
        return True        
        return False  
my_valid = my_w.register(validate)
e1 = Entry(my_w,validate='focusout',validatecommand=(my_valid,'%P'))
e2 = tk.Entry(my_w,width=10)
b1 = tk.Button(my_w,text='Submit')
my_w.mainloop()  # Keep the window open

List of validate options

focusWhen entry widget get or lose focus
focusinWhen entry widget get focus
focusoutWhen entry widget get focus
keyWhen entry widget keystroke changes the widget's contents
allAll of the above events
noneNo validation ( Not same as keyword None)

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer