Tkinter binding mouse double click
Asked Answered
D

3

14

I'm trying to bind my mouse double click to a function which for now just prints the current selection in a Tkinter list box. To be clear the function should only print when the user double clicks on one of the items of a Tkinter list box. What event binding should I use?

Dimitry answered 3/12, 2014 at 2:19 Comment(0)
P
37

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.

Passable answered 3/12, 2014 at 2:21 Comment(1)
As a note, if you have a bind for a single click and double click, both bindings will be called.Chanticleer
P
7

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.

Prostitution answered 8/3, 2016 at 0:3 Comment(0)
P
4

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()
Purgation answered 31/5, 2018 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.