Entry Field Validation in Tkinter Python
In this article, you will learn about the entry field validation using the Tkinter Python.
Tkinter is a standard cross-platform package for creating graphical user interfaces (GUIs). This creates a window for your entire GUI application. This windowing object can contain different widgets, like- text labels, buttons, radio buttons, etc. If you want to know more about the Tkinter in detail, please click on the following link-
Python Tkinter Overview and ExamplesImport Tkinter and create object
First, we need to import the Tkinter module and create a top level object using the Tk class.
from tkinter import *
root = Tk()
Install and Import Pmw
Pmw (Python Mega Widget) is a toolkit for building high-level compound widgets and mega widgets. It provides a convenient way to add functionality to a Tkinter application. If you do not have this module installed, open the terminal window and run the following command-
(env) c:\python37\Scripts\projects>pip install Pmw
On successful installation, it returns something like this-
(env) c:\python37\Scripts\projects>pip install Pmw
Successfully built Pmw
Installing collected packages: Pmw
Successfully installed Pmw-2.0.1
Next, we will import and initialise this module-
import Pmw
Pmw.initialise()
Entry Fields Validation
Python provides a standard Entry widget to enter or display a single line of text. Here is a simple example of the EntryField widget without validation.
f1 = Pmw.EntryField(root, labelpos=W, label_text='Enter Text',
validate = None)
The above code creates a text entry field without any validation.
Python validate the Real Integer
Here, we are validating the real input. It accepts input only in the range between 96.0 to 107.0.
realno = Pmw.EntryField(root, labelpos=W, value = '98.5',
label_text = 'Real (96.0 to 107.0):',
validate = {'validator' : 'real',
'min' : 96, 'max' : 107, 'minstrict' : 0})
Python validate the Integer
The below code validates the integer input. The entry field accepts an integer value only in the range between 10 and 40.
int_val = Pmw.EntryField(root, labelpos=W, label_text = 'Integer (10 to 40):',
validate = {'validator' : 'numeric',
'min' : 10, 'max' : 40, 'minstrict' : 0},
value = '15')
Python validate the Date input
The below code validates the date input. It only accepts the date of the year 2019.
date_val = Pmw.EntryField(root, labelpos=W, label_text = 'Enter Date (In 2019)',
value = '2019/01/10', validate = {'validator' : 'date',
'min' : '2019/01/01', 'max' : '2019/12/31'})
Complete Code
Here, we have merged all the above code and run it on a terminal window.
from tkinter import *
import Pmw
root = Tk()
Pmw.initialise()
val_non = Pmw.EntryField(root, labelpos=W, label_text='Enter text',
validate = None)
realno_val = Pmw.EntryField(root, labelpos=W, value = '98.5',
label_text = 'Enter Real (96-107)',
validate = {'validator' : 'real',
'min' : 96, 'max' : 107, 'minstrict' : 0})
int_val = Pmw.EntryField(root, labelpos=W, label_text = 'Enter Integer (10-40)',
validate = {'validator' : 'numeric',
'min' : 10, 'max' : 40, 'minstrict' : 0},
value = '15')
date_val = Pmw.EntryField(root, labelpos=W,label_text = 'Enter Date',
value = '2019/01/10', validate = {'validator' : 'date',
'min' : '2019/01/01', 'max' : '2019/12/31'})
widgets = (val_non, realno_val, int_val, date_val)
for widget in widgets:
widget.pack(fill=X, expand=1, padx=10, pady=5)
Pmw.alignlabels(widgets)
real.component('entry').focus_set()
root.mainloop()
When you learn the above code, it returns the following output-

Related Articles
Python Tkinter Combobox Event BindingPython Tkinter Combobox
Add background image in Python Tkinter
Python Tkinter Text Widget
Countdown clock and timer using Tkinter in Python
Python Tkinter Frame Widget
Python Tkinter Checkbutton Widget
Entry Field Validation in Tkinter Python
Python Tkinter Tutorial with Examples
Python Tkinter Scale Widget
Python3 Tkinter Messagebox
Python 3 Tkinter Menu Bar
Python Tkinter Geometry Managers
Python get visitor information by IP address
Python OpenCV ColorMap
Python gmplot to add google map on a web page