python - [Tkinter]将键盘键绑定到单选按钮
问题描述
我正在尝试将F1
密钥绑定到我的 tkinter GUI 中的单选按钮。我试过这个
import tkinter as tk
def stack():
print('StackOverflow')
root = tk.Tk()
v = tk.IntVar()
RadBut = tk.Radiobutton(root, text = 'Check', variable = v, value = 1, command = stack)
RadBut.pack()
RadBut.bind_all("<F1>", stack)
root.mainloop()
如果我运行它并尝试按 f1 它给出
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\MyName\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
TypeError: stack() takes 0 positional arguments but 1 was given
但是用鼠标点击按钮可以正常工作。
提前致谢
解决方案
就像@acw1668 所说,你需要给stack()
函数一个参数。
我想补充一点,它不一定是None
.
这是您的代码:
from tkinter import *
def stack(event = "<F1>"):
print('StackOverflow')
root = Tk()
v = IntVar()
RadBut = Radiobutton(root, text = 'Check', variable = v, value = 1, command = stack)
RadBut.pack()
RadBut.bind_all("<F1>", stack)
root.mainloop()
希望这可以帮助!
推荐阅读
- instagram - 通过查询搜索来搜索所有 Instagram 用户
- javascript - map() 内部的条件不能按预期工作(React)
- python - Django 模型 hh:mm 字段
- c# - 无法从 Windows 窗体 DataGridView C# 获取数据
- c# - 从 Helix Tookit 保存图像
- cuda - 我有哪个 Cuda 版本?Conda 虚拟环境的奇怪行为
- asp.net-core - asp.net 核心在地址栏上显示正确的 url
- android - Android - AlphaAnimation - FadeOut ImageView 比 FadeIn 其他 ImageView
- javascript - 在 JavaScript 中将字符串拆分为列表
- excel - Excel 中的平均值