【发布时间】:2020-05-19 03:26:44
【问题描述】:
我正在尝试将F1key 绑定到我的 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
但是用鼠标点击按钮可以正常工作。
提前致谢
【问题讨论】:
-
bind的回调函数将接收事件对象作为参数。所以给stack()添加一个参数:def stack(event=None),那么bind和command都可以使用。
标签: python button tkinter bind