Skip to content
Related Articles

Related Articles

How to make specific text non-removable in tkinter?

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 18 Mar, 2022

In this article, we will discuss how to make specific text non-removable in Tkinter. But before that let us know what Tkinter provides us.


To install this module type the below command in the terminal.

pip install tk

Entry Validate Operation in Tkinter

Before proceeding with Non-removable text syntax, one must understand how Entry Validate works. The task of the validate command is to restrict the text that can be entered inside an Entry widget. 


import tkinter as tk
def text_only(entered_data):
    return entered_data.isalpha()
root = tk.Tk()
# the dimensions of the GUI window
root.resizable(False, False# fixed size for GUI
# create a function and pass in register
# the function is to check the validation of Tkinter Box
valid = root.register(text_only), '%S'
entry_box = tk.Entry(validate="key", validatecommand=valid)
# end GUI window with root.mainloop()

The specialty about the above code is the Entry Box accepts only alphabets i.e. Letters. If you try entering a digit or a special character it won’t accept it. Key points to take away are these two lines of code.

valid = root.register(text_only),'%S'
entry_box = tk.Entry(validate="key", validatecommand=valid)

Valid is a variable that is assigned to the validation function for the entry box. That means root.register takes a function as a parameter. The root.register() method returns a string(only alpha) that is assigned to a variable ‘valid’ that is used to call the callback function in the later stages when the keystroke is triggered in an Entry box. ‘%S’ in the validatecommand option means that the inserted or deleted character is passed in argument to the text_only() function. Inside the Entry widget, we assign a validate command to a key. The “key” value specifies that validation occurs whenever any keystroke(input from keyboard) changes the widget’s contents. Now when a key is pressed on the entry widget validatecommand is triggered which performs text_only() function operation. 

With this logic in mind let us proceed with the final phase of making a text non-removable. 

Non-Removable Text In Tkinter

In order to make a specific text unremovable, we shall create an Entry Widget. Insert a message inside an Entry Widget which as to be non-removable and then create a function that checks the condition if the Entry widget starts with that specific message. And this function is passed as an argument inside the root.register method. This time we pass “%P” which denotes the value that the text will have if the change is allowed. Entry Widget should be passed with the validate command that triggers keystroke. 



import tkinter as tk
def non_removable_text(text):
    # Enter Your Name is non-removable Text
    return text.startswith("Enter Your Name:")
root = tk.Tk()
root.resizable(False, False)
# define a function with non-removable text
# '%P' denotes the value that the text will
# have if the change is allowed.
valid = root.register(non_removable_text), "%P"
# create a Entry widget with a validate of key
# key-specify keystroke
entry = tk.Entry(validate='key', validatecommand=(valid))
# add a message
entry.insert("end", "Enter Your Name:"), y=100, width=300)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!