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("www.plus2net.com") # 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')
l1.grid(row=1,column=1,padx=20,pady=20)
e1 = Entry(my_w,validate='key',validatecommand=(my_valid,'%S'))
e1.grid(row=1,column=2,padx=20)
my_w.mainloop() # Keep the window open
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 |
%P
instead of %S
. For some checking we have to consider the whole string like istitle()validatecommand=(my_valid,'%P')
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("www.plus2net.com") # Adding a title
def validate(u_input):
if(re.search(regex,u_input) and u_input.isalpha):
print(True)
b1.config(state='active')
return True
else:
print(False)
b1.config(state='disabled')
return False
my_valid = my_w.register(validate)
l1=tk.Label(my_w,text='Email')
l1.grid(row=1,column=1,padx=5,pady=20)
e1 = Entry(my_w,validate='focusout',validatecommand=(my_valid,'%P'))
e1.grid(row=1,column=2,padx=10)
l2=tk.Label(my_w,text='Name')
l2.grid(row=1,column=3,padx=5,pady=20)
e2 = tk.Entry(my_w,width=10)
e2.grid(row=1,column=4)
b1 = tk.Button(my_w,text='Submit')
b1.grid(row=1,column=5)
my_w.mainloop() # Keep the window open
print(type(4.5))
focus | When entry widget get or lose focus |
focusin | When entry widget get focus |
focusout | When entry widget get focus |
key | When entry widget keystroke changes the widget's contents |
all | All of the above events |
none | No validation ( Not same as keyword None) |
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.