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

TkinterPythonGUI-Programming

The Text widget accepts multiline user input, where you can type text and perform operations like copying, pasting, and deleting. There are certain ways to disable the shortcuts for various operations on a Text widget.

In order to disable copy, paste and backspace in a Text widget, you’ve to bind the event with an event handler and return break using lambda keyword in python. The following example demonstrates how it works.

Example

# Import the required library
from tkinter import *

# Create an instance of tkinter frame or widget
win=Tk()
win.geometry("700x350")

# Create a text widget
text=Text(win, font="Calibri, 14")
text.pack(fill= BOTH, expand= True)

# Bind the keys with the event handler
text.bind('<Control-v>', lambda _:'break')
text.bind('<Control-c>', lambda _:'break')
text.bind('<BackSpace>', lambda _:'break')

win.mainloop()

Output

Running the above code will display a window with a Text widget where the user can type and insert text.

However, it won't allow the user to use the <BackSpace> key or a combination of "Ctrl+C" and "Ctrl+V" keys.

raja
Updated on 16-Dec-2021 11:19:01

Advertisements