- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to interactively validate an Entry widget content in tkinter?
Validating the content is a necessary part of any featured application where we allow only the required data to be processed. An Entry Widget in tkinter is used to display single line text Input. However, we can validate the Entry widget to accept only digits or alphabets.
Let us first create an Entry widget that accepts only digits input. So initially, we will create an Entry widget and using register(callback) function, we will call to validate the Entry widget which validates whenever a key is stroked. It returns a string that can be used to call a function. Then, calling the callback function to validate the entry widget as,
Validatecommand supports other values such as, %d, %i, %W, %P, %s, %v, etc.
#Import the required library from tkinter import * #Create an instance of tkinter frame or window win = Tk() def callback(input): if input.isdigit(): print(input) return True elif input=="": print(input) return True else: print(input) return False #Create an entry widget entry= Entry(win) fun= win.register(callback) entry.config(validate="key", validatecommand=(fun, '%P')) entry.pack() win.mainloop()
Running the above code will display a window that contains an Entry widget which accepts only digits and prints the key strokes on the console.
In the output screen, enter any character. You will find that you can enter only digits, thus the entry widget is validated.
- How to disable an Entry widget in Tkinter?
- How to clear Text widget content while clicking on the Entry widget itself in Tkinter?
- How to set the text/value/content of an 'Entry' widget using a button in Tkinter?
- How to get the value of an Entry widget in Tkinter?
- How to use the Entry widget in Tkinter?
- How to insert the current time in an Entry Widget in Tkinter?
- How to use a StringVar object in an Entry widget in Tkinter?
- How to set focus on Entry widget in Tkinter?
- How to connect a variable to the Tkinter Entry widget?
- How to set default text for a Tkinter Entry widget?
- How to set the font size of Entry widget in Tkinter?
- How to insert a temporary text in a tkinter Entry widget?
- Get contents of a Tkinter Entry widget
- Getting the Cursor position in Tkinter Entry widget
- How to change the Entry Widget Value with a Scale in Tkinter?