- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to stop copy, paste, and backspace in text widget in tkinter?
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.
Advertisements