2017-02-23 79 views

回答

3

默认行为被实现为内部tk类的绑定。对于按钮,该类是"Button"

要添加新的行为,您可以在类名上使用bind_class,假定您想要所有tkinter按钮的此行为。同样,要删除默认行为,可以将unbind_class与类名一起使用。您必须在创建根窗口后执行此操作。

import Tkinter as tk # python 2.7 
# import tkinter as tk # python 3.x 

root = tk.Tk() 

# invoke the button on the return key 
root.bind_class("Button", "<Key-Return>", lambda event: event.widget.invoke()) 

# remove the default behavior of invoking the button with the space key 
root.unbind_class("Button", "<Key-space>") 
+0

是的,这将做到这一点 – user3364161