Validating Entry Widget in Python Tkinter
The Python programming language provides a range of frameworks to work with Graphical User Interface (GUI) applications. The Tkinter or Tk interface is among Python's most commonly utilized interfaces to construct applications based on Graphical User Interface (GUI). Some applications require validation of the text fields to prevent invalid input from the user before the form is submitted. Python enables the validation of the input by allowing the tracing of the variable with the help of a callback function. This function is called when input is added to/deleted from an Entry widget of the Tkinter package.
In the following tutorial, we will learn how to validate the user inputs using the Tkinter package.
So, let's get started.
An Introduction to the Tkinter Validation
Validation in Python Tkinter means declaring the user inputs officially acceptable. In other words, validation is referred to as checking whether the information shared by the user meets the requirements.
There are primarily three types of Validations:
The Validation in Python Tkinter depends upon three arguments or parameters that we can utilize for any input widget like the Entry() widget:
Let us now briefly understand some basic concepts of these arguments or commands.
The validate argument
The validate argument can be one of the string values as follows:
The validatecommand argument
The validatecommand argument is a tuple consisting of:
We will pass a callable to the widget_name.register() method in order to get a reference to a Tcl/Tk function. This method will return a string we can utilize with the validatecommand option.
Let us consider the following table that displays the substitution codes which we can utilize with the tuple:
Let us consider the following example to construct a validatecommand option using the self.validate() method and %P substitution code:
The invalidcommand argument
Similar to validatecommand argument, the invalidcommand argument also requires the utilization of the widget_name.register() method and the substitution code.
Let us consider the following example that returns a tuple by passing into the invalidcommand option:
An Example based on Tkinter Validation
In this example, we will create a form consisting of an e-mail input. If the entered e-mail address is invalid, it will display an error message and change the color of the e-mail input. Moreover, we will trigger the validation event when the focus moves out of the entry.
Let us consider the following snippet of code to understand the implementation of the above statement.
Output 1: If the entered e-mail address is valid.
Output 2: If the entered e-mail address is not valid.
How does validation work in the above example?
Firstly, we have created a validate command with the help of the self.validate() method and %P substitution code.
Secondly, we have created the invalidcommand command that utilizes the self.on_invalid() method.
Next, we have configured the entry widget that utilizes the validate, validatecommand, and invalidcommand parameters.
Moreover, we have defined the displayMessage() method that alters the text of the error_label widget and the text color of the email_field widget.
After that, we have defined another method as validate() that performs validation of the value of the email_field widget.
The validate() method in the above snippet of code returns True if the input text is valid; otherwise, it returns False. If the entered text is a valid e-mail address, call the displayMessage() method to hide the error message and set the color of the text to black.
If the entered text turns out to be an invalid e-mail address, Tkinter will run the on_invalid() method.
At last, we have defined the on_invalid() method to display an error message and set the color of the text of the email_field widget to red.
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week