Tkinter binding mouse double click
You can bind to <Double-Button-1>
:
widget.bind('<Double-Button-1>', handler)
There is also <Button-1>
for normal mouse clicks and <Triple-Button-1>
for a triple mouse click.
For more information on bindings in Tkinter, see Events and Bindings.
You have to realize that there is a hierarchy to all widgets, and this means that for each widget you click, multiple bindings are possible. If you don't override the default action, each hierarchy's default handler gets called, starting at the lowest level (such as your Listbox
) and going all the way up to the Tk()
or Toplevel()
widget. For you, since you want to print only when a listbox item is clicked, you can bind to the listbox widget, as follows:
listboxWidget.bind('<Double-Button-1>', listboxWidget_leftclick_handler)
Then, when you enter the def listboxWidget_leftclick_handler(event)
function, you don't have to check the event.widget
value to see if it's the name of your Listbox
widget. But you could also check at a higher level (bind a handler to a higher-level widget) and check event.widget
to see which widget was clicked.
Also note that the only way to prevent the entire hierarchy of event handlers from triggering is by using a return 'break'
from your custom handler, but you usually only need to do this if later handlers corrupt what your custom handler has done.
Additional info about default handlers
The other part which I left out is that there is also a "default" handler for most events. If you bind your own handler, once it's finished, if you don't return 'break'
, the default handler will be called next.
For example, say you want to make your own Entry box into a password entry. By default, when you type alphanumeric chars when the Entry has focus (which means it's getting input from the keyboard), the chars will appear in the Entry
. You can bind:
myEntry.bind('<KeyPress>', passworder)
where passworder
is your custom handler which grabs the event holding your inputted char and then outputs an asterisk into the Entry
instead. But, if you don't use a return "break"
at the end of your handler, the Entry
widget is still going to see that char that you didn't want shown, because once your handler is done inserting the asterisk, the default handler will simply insert the typed char (like it would normally). But, if you do the return 'break'
, the default handler won't get called, and the typed char(s) won't appear in the Entry
.
As an add-on. In order to distinguish action between a single click and a double click, delay the call to mouse action for a brief period to allow for the double click flag to be set. See below example:
from tkinter import *
def mouse_click(event):
''' delay mouse action to allow for double click to occur
'''
aw.after(300, mouse_action, event)
def double_click(event):
''' set the double click status flag
'''
global double_click_flag
double_click_flag = True
def mouse_action(event):
global double_click_flag
if double_click_flag:
print('double mouse click event')
double_click_flag = False
else:
print('single mouse click event')
root = Tk()
aw = Canvas(root, width=200, height=100, bg='grey')
aw.place(x=0, y=0)
double_click_flag = False
aw.bind('<Button-1>', mouse_click) # bind left mouse click
aw.bind('<Double-1>', double_click) # bind double left clicks
aw.mainloop()