Open In App

How to stop copy, paste, and backspace in text widget in tkinter?

Last Updated : 22 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to stop backspacing, copying, and pasting in the text widget in Tkinter.

In order to disable copying, pasting, and backspacing in Tkinter a Text widget, you’ve to bind the event with an event handler, binding helps us to create complex GUI applications that bind some specific key to functions, which execute when that key is pressed.

Stepwise Implementation:

Step 1: First of all, import the library Tkinter.

from tkinter import *

Step 2: Now, create a GUI app using Tkinter

app=Tk()

Step 3: Next, give dimensions to the app.

app.geometry("# Dimensions of the app")

Step 4: Moreover, create and display the text widget for the GUI app.

text=Text(app, font="#Font-Style, #Font-Size")
text.pack(fill= BOTH, expand= True)

Step 5: Further, bind the paste key so that the user can’t copy anything when he presses Control-V together.

text.bind('<Control-v>', lambda _:'break')

Step 6: Later on, bind the copy key so that the user can’t copy anything when he selects and press Control-C together.

text.bind('<Control-c>', lambda _:'break')

Step 7: Then, bind the backspace key so that the user can’t go back and clear anything when he presses the Backspace button. 

text.bind('<BackSpace>', lambda _:'break')

Step 8: Finally, make an infinite loop for displaying the app on the screen.

app.mainloop()

Example:

Python3




# Python program to stop, copy, and backspace
# in a text widget in Tkinter
  
# Import the library tkinter
from tkinter import *
  
# Create a GUI widget
app=Tk()
  
# Set the geometry of the app
app.geometry("700x350")
  
# Create and display text field widget
text=Text(app, font="Calibri, 14")
text.pack(fill= BOTH, expand= True)
  
# Bind the paste key with the event handler
text.bind('<Control-v>', lambda _:'break')
  
# Bind the copy key with the event handler
text.bind('<Control-c>', lambda _:'break')
  
# Bind the backspace key with the event handler
text.bind('<BackSpace>', lambda _:'break')
  
# Make infinite loop for displaying app
# on the screen
app.mainloop()


Output:

How to stop copy, paste, and backspace in text widget in tkinter?

 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads