3

I need to bind command button-click to a function in python. I have already bound the flag function to right click, but this only work when right-clicking with a mouse. As I write the majority of my code on a laptop, this is horribly inconvenient. Here is what I currently have:

    # set up the mouse click listeners
    self.bind('<Button-1>',self.expose)
    self.bind('<Button-2>',self.flag)
    #this is where I want to bind self.flag to command click   

I'd like to use self.bind if possible, and simply bind command click to self.flag. Is it possible to do this?

0

1 Answer 1

5

You can just use:

self.bind("<Command-Button-1>",self.flag)

This can be done in addition to <Button-2> and you may want to also bind <Control-Button-1> for compatibility.

I would normally link to http://infohost.nmt.edu/tcc/help/pubs/tkinter/event-modifiers.html but it seems to be down at the moment, basically there are a few modifiers that can be used in combination with Button or Key:

Alt      True when the user is holding the alt key down.
Any      This modifier generalizes an event type. For example, the event pattern '<Any-KeyPress>' applies to the pressing of any key.
Control  True when the user is holding the control key down.
Double   Specifies two events happening close together in time. For example, <Double-Button-1> describes two presses of button 1 in rapid succession.
Lock     True when the user has pressed shift lock.
Shift    True when the user is holding down the shift key.
Triple   Like Double, but specifies three events in rapid succession.

So you could for example bind <Control-Shift-Double-Button-1> if your program needed to be that elaborate.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it worked! I also added shift click for super-compatability, as well as control and command. Because I'm paranoid.
Nice, it is also worth mentioning that on unix (mac and linux etc.) <Button-2> is right click and <Button-3> is the middle mouse button but on windows computers they are switched. So you may also want to bind <Button-3> too if you are really paranoid (or check sys.platform)
Also @JosiahSchmidt I assume you accept my answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.