Creating a Tkinter entry integer only widget

No one can deny the importance of user interface in GUI programming. It beautifies the programming experience. However, it is considered a difficult job to handle user input in GUI programming and it becomes even more complicated if you wish to use specific formats. The main purpose of our discussion will be creating a Tkinter entry integer only widget in Python application as it accepts integer values only. The program would use integer numbers with this method which will result in accurate data. Also, it will rationalize the user interaction.

Tkinter: Simplifying GUI Development

For a tkinter entry integer only widget creation, use the following code and let’s illustrate the code.

				
					import tkinter as tk

def validate_input(P):
    if P == "" or P.isdigit():
        return True
    else:
        return False

root = tk.Tk()
root.title("entry integer only")

validate_input_cmd = root.register(validate_input)

entry = tk.Entry(root, validate="key", validatecommand=(validate_input_cmd, "%P"))
entry.pack(padx=20, pady=20)

root.mainloop()

				
			
Understanding the Code
  1. Firstly, import the ‘tkinter’ library, the base of the GUI programming in other words. ‘validate_input’ is the necessary element which takes and validate the input that is ‘(P)’. It also makes sure that this value must meet the criterion.  In case of successful or valid digital form of data, you receive True message. Else False message appears.

  2. After this, create main application window with ‘tk.Tk()’ . For your ease, name it “Integer Entry.”  The ‘register’ method will enable it with the help of ‘validate_input’ function.

  3. Next, ‘tk.Entry()’ is used for widget entry. It also configures to activate the ‘validate_input’ function for all keystroke. For this purpose, it uses validatecommand option. Lastly, pack this entry widget into the window and initiate the main event loop with ‘root.mailyloop()’ .
Benefits of an Integer-Only Entry Widget

The integer-only entry widget serves many purposes like it makes user experience better and also supports to input data and data validation. When you apply use of integer data only, it improves performance of application as it is secured from data corruption that may due to the inaccurate data. This streamlines the user interaction with the application with the accurate data.

Input numbers 0-20 in tkinter or any specific number

If you want to know how the inputs to an entry widget in tkinter to 0-20. Then you try modify above code as like this:

				
					import tkinter as tk

def validate_input(P):
    if P == "" or (P.isdigit() and 0 <= int(P) <= 20):
        return True
    else:
        return False

root = tk.Tk()
root.title("Entry Integer Only (0-20)")

validate_input_cmd = root.register(validate_input)

entry = tk.Entry(root, validate="key", validatecommand=(validate_input_cmd, "%P"))
entry.pack(padx=20, pady=20)

root.mainloop()

				
			

If you want to restrict users to any specific number, You can modify this line of code: replace any number here: <= 20

				
					 if P == "" or (P.isdigit() and 0 <= int(P) <= 20):
				
			
Conclusion 

Creating a tkinter entry integer only widget with Tkinter authorize developers to deal with all type of input and to maintain it in a very useful manner. Our user experience is enhanced with Tkinter library. When you use whole numbers for the input integers, it results in data accuracy. In this article, we have learned the use of this function.  

1 thought on “Creating a Tkinter entry integer only widget”

Leave a Comment