Python Tkinter命令单击

时间:2016-04-26 21:18:28

标签: python tkinter

我需要将命令按钮单击绑定到python中的函数。我已经将标志功能绑定到右键,但这只有在用鼠标右键单击时才有效。当我在笔记本电脑上编写大部分代码时,这非常不方便。这是我现在拥有的:

    # 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   

如果可能,我想使用self.bind,只需将命令单击绑定到self.flag即可。是否有可能做到这一点?

1 个答案:

答案 0 :(得分:0)

你可以使用:

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

除了<Button-2>之外,您还可以执行此操作,并且您可能还希望绑定<Control-Button-1>以获得兼容性。

我通常会链接到http://infohost.nmt.edu/tcc/help/pubs/tkinter/event-modifiers.html,但目前似乎有所下降,基本上有一些修饰符可以与ButtonKey结合使用:

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.

因此,如果您的程序需要精心设计,那么您可以绑定<Control-Shift-Double-Button-1>