在 tkinter 中,您可以使用空格键按下突出显示的按钮。如何将其更改为返回键?我不想将特定功能绑定(bind)到按钮,我想在按钮突出显示时更改按下按钮的键。
最佳答案
默认行为是作为内部 tk 类的绑定(bind)实现的。对于按钮,该类是 "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>")
关于python - Tkinter 使用 enter 而不是空格键按下按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42406164/