How to configure default Mouse Double-Click behavior in a Tkinter text widget?


The Text widget in Tkinter is used to add a text editor-like functionality in the application. The Text widget supports multiline user input from the user. We can configure the text widget properties such as its font properties, text color, background, etc., by using the configure() method.

The Text widget also provides tagging through which we can make a selection of text. To extend this functionality, we can also bind the Double-Click button that will possess the event for selecting a Word at a time.

Example

Let us have a look at the example, where we have disabled the double mouse-click button to select the text.

# Import the required libraries
from tkinter import *

# Create an instance of tkinter frame or window
win = Tk()

# Set the size of the tkinter window
win.geometry("700x350")

# Define a function to get the length of the current text
def select_all():
   text.tag_add("start", "1.0", "end")
   return "break"

# Create a text widget
text = Text(win, width=50, height=10, font=('Calibri 14'))
text.pack()
text.insert(INSERT, "Select a word and then double-click")

# Bind the buttons with the event
text.bind('<Double-1>', select_all)

win.mainloop()

Output

Running the above code will display a text widget with a predefined text. Now, select a word and double-click on it to select the word.

Updated on: 06-Aug-2021

374 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements