quickbug
quickbug

Reputation: 4848

Tkinter Text: calling a custom function with the word selected by double-click

When the user double click a word in a Text widget, some internal callback is called, which results in the selection of a word. I want to have some additionnal task to be done on that word. How can I do that? The naive idea was to bind my own callback to the double click. It did not worked, because of a wrong priority: my callback was executed first, before the word is selected, and the Text callback wascalled after. How can I solve that?

An idea would be to retreive a handle to the existing callback and reuse it after:

actual_callback = **<how can I get this handle?>**    
text.bind("<Double-Button-1>", my_callback)

then my callback would straightforwardly write as follows:

def my_callback(event):
    actual_callback(event) # this will select the word
    w = text.selection_get()
    do_something(w)

Please help to finalize it this way, or feel free to propose a better way of acheiving my original goal.

Upvotes: 0

Views: 536

Answers (1)

Novel
Novel

Reputation: 13729

The easy answer is to simply wait a beat before running your callback.

text.bind("<Double-Button-1>", lambda e: text.after(2, my_callback, e)) # wait 2 ms before running callback

Upvotes: 3

Related Questions