[Tkinter]将键盘琴键绑定到单选按钮

问题描述 投票:0回答:1

我正在尝试将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

但是用鼠标单击按钮效果很好。

预先感谢

python button tkinter bind
1个回答
0
投票
[就像@ 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()

希望这会有所帮助!
© www.soinside.com 2019 - 2024. All rights reserved.