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 and provides various configuration options. 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 bind the Double-Click button event to customize the selection behavior, such as selecting a word or entire text at a time.

Default Double-Click Behavior

By default, double-clicking in a Text widget selects the word under the cursor. We can override this behavior by binding a custom function to the <Double-1> event.

Example 1: Select All Text on Double-Click

Let us look at an example where we override the default double-click behavior to select all text instead of just a word ?

# 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 select all text
def select_all(event):
    text.tag_add("sel", "1.0", "end")
    return "break"

# Create a text widget
text = Text(win, width=50, height=10, font=('Calibri', 14))
text.pack()
text.insert(INSERT, "This is sample text. Double-click anywhere to select all text.")

# Bind the double-click event with the custom function
text.bind('<Double-1>', select_all)

win.mainloop()

Example 2: Select Current Line on Double-Click

Here's another example that selects the entire current line when double-clicking ?

from tkinter import *

win = Tk()
win.geometry("700x350")

def select_line(event):
    # Get the current cursor position
    cursor_pos = text.index(INSERT)
    
    # Get line number
    line = cursor_pos.split('.')[0]
    
    # Select the entire line
    text.tag_remove("sel", "1.0", "end")
    text.tag_add("sel", f"{line}.0", f"{line}.end")
    return "break"

text = Text(win, width=50, height=10, font=('Calibri', 14))
text.pack()

sample_text = """Line 1: This is the first line
Line 2: This is the second line  
Line 3: This is the third line
Line 4: Double-click any line to select it"""

text.insert("1.0", sample_text)

# Bind double-click to select entire line
text.bind('<Double-1>', select_line)

win.mainloop()

Example 3: Disable Double-Click Selection

To completely disable the double-click selection behavior, return "break" without performing any selection ?

from tkinter import *

win = Tk()
win.geometry("700x350")

def disable_double_click(event):
    # Return "break" to prevent default behavior
    return "break"

text = Text(win, width=50, height=10, font=('Calibri', 14))
text.pack()
text.insert(INSERT, "Double-clicking is disabled in this text widget.")

# Bind double-click to disable selection
text.bind('<Double-1>', disable_double_click)

win.mainloop()

Key Points

  • The <Double-1> event represents a double-click with the left mouse button
  • Returning "break" from the event handler prevents the default behavior
  • Use text.tag_add("sel", start, end) to create text selections
  • Use text.tag_remove("sel", "1.0", "end") to clear existing selections

Conclusion

You can customize double-click behavior in Tkinter Text widgets by binding the <Double-1> event to a custom function. Return "break" from your handler to override the default word-selection behavior and implement your own logic.

Updated on: 2026-03-26T00:15:23+05:30

849 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements