python - Tkinter 使用 enter 而不是空格键按下按钮

标签 python tkinter

在 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/

相关文章:

python - 错误 : ‘memcpy’ was not declared in this scope (Ubuntu 16. 04,opencv2.4.13)

Python通过逐行读取文件将多行读成单行

python - 如何在 Tkinter 中将图像 (.png) 放置在 `LabelFrame` 中并调整其大小?

python - 在 Tkinter 中将 Canvas 提升到其他 Canvas 之上

python - SQLAlchemy 中的僵尸连接

python - 在 Python 中打印的三种方法——何时使用每种方法?

python - 如何提取和打印与这些 RegEx 模式之一匹配的子字符串,同时保持原始输入字符串中的出现顺序?

python - 类型错误: '>' 和 'function' 实例之间不支持 'int'

python - 如何增加 Tkinter 最大 Canvas 尺寸以获得超大图像?

python-3.x - Tkinter 按钮只能工作一次