3
在tkinter中,您可以按空格键突出显示的按钮。我如何将其更改为返回键?我不想将特定功能绑定到按钮上,如果按钮突出显示,我想更改按下按钮的按钮。Tkinter按下输入键而不是空格键
在tkinter中,您可以按空格键突出显示的按钮。我如何将其更改为返回键?我不想将特定功能绑定到按钮上,如果按钮突出显示,我想更改按下按钮的按钮。Tkinter按下输入键而不是空格键
默认行为被实现为内部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>")
是的,这将做到这一点 – user3364161