Last Updated : 12 Jul, 2025
Python offers a variety of frameworks to work with GUI applications.
Tkinteror Tk interface is one of the most widely used Python interface to build GUI based applications. There are applications that require validation of text fields to prevent invalid input from the user before the form is submitted. Python allows input validation by allowing variable tracing using a callback function. This function is called whenever an input is added/deleted to/from an Entry widget. Some applications validate input on form submission, but the following piece of code performs validation with every stroke of key from the keyboard.
import tkinter
from tkinter import *
def callback(input): if input.isdigit(): print(input) return True elif input is "": print(input) return True else: print(input) return FalseExplanation The callback function checks the input in the Entry widget for valid entry. If the entry is valid it returns True else False. In this example, the input entered through the keyboard is checked for numeric type. If the input is numeric type then the callback function returns true. For deletion operation the callback function returns true as input is "" . However for any non-numeric input the callback function returns false.
root=Tk()Syntax: Tk(screenName=None, baseName=None, className='Tk', useTk=1) Parameter: In this example, Tk class is instantiated without arguments. Explanation This method creates a parent widget which usually is the main window of an application.
e=Entry(root)Syntax: Entry(master, **options) Parameter:
e.place(x=50, y=50)Syntax: place(x, y) Parameter:
reg=root.register(callback)Syntax: register(function) Parameter:
e.config(validate="key", validatecommand=(reg, '%P'))Syntax: config(validate="key", validatecommand=(reg, '%P')) Parameter:
root.mainloop()Syntax: mainloop() Explanation The mainloop() is an infinite loop used to run the application as long as the window is not closed.
import tkinter
from tkinter import *
def callback(input):
if input.isdigit():
print(input)
return True
elif input is "":
print(input)
return True
else:
print(input)
return False
root = Tk()
e = Entry(root)
e.place(x = 50, y = 50)
reg = root.register(callback)
e.config(validate ="key",
validatecommand =(reg, '% P'))
root.mainloop()
Output:
(Full Screen View Recommended)
ExplanationWhen we enter digits using our keyboard the callback function returns true and the value is allowed in the entry widget. However on entering alphabets from the keyboard the callback function returns false and the value is not allowed to entered in the entry widget. To get a clearer idea about the working of the callback function the input sent through the keyboard is printed on the console. It can be seen that any non-numeric input gets printed on the console but is not allowed in the entry widget. Also both insertion and deletion of numerics are allowed in the entry widget.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4