How to stop copy, paste, and backspace in text widget in tkinter?
Last Updated :
22 Apr, 2022
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
from tkinter import *
app = Tk()
app.geometry( "700x350" )
text = Text(app, font = "Calibri, 14" )
text.pack(fill = BOTH, expand = True )
text.bind( '<Control-v>' , lambda _: 'break' )
text.bind( '<Control-c>' , lambda _: 'break' )
text.bind( '<BackSpace>' , lambda _: 'break' )
app.mainloop()
|
Output:
Please Login to comment...