Tkinter 按钮只有在按下左键时才会下沉

Tkinter button only sinks when pressed with a left click

这里是 Tkinter 初学者

我一直在做一个虚拟鼠标项目,在创建鼠标功能对应的GUI时偶然发现了一个小问题。具体来说,我有一个功能,我想在单击 UI 按钮时执行,当按下按钮时按钮下沉,而其他按钮保持上升状态。回调函数按预期适用于每个鼠标按钮,但问题是 UI 按钮仅在单击鼠标左键时才会下沉。

有没有办法让 UI 按钮下沉,而不管我按下哪个鼠标按钮?例如,我想使用鼠标中键制作一个 UI 按钮下沉。

这是我的代码,使用 python 3.8:

import tkinter as tk


root = tk.Tk()

switch_frame = tk.Frame(root)
switch_frame.pack()



root.geometry("230x200")

def hh(event,):
    print('This works as intended with all the mouse buttons, but the UI button only sinks when pressed with a left click')
   

switch_variable = tk.StringVar(value="off")

b1 = tk.Radiobutton(switch_frame, text='Left Click',width=15,height=6,variable=switch_variable,indicatoron=False,value="left_click")
b2 = tk.Radiobutton(switch_frame, text='Right Click',width=15,height=6,variable=switch_variable,indicatoron=False,value="right_click")
b3 = tk.Radiobutton(switch_frame, text='Double Click',width=15,height=6,variable=switch_variable,indicatoron=False,value="double_click")
b4 = tk.Radiobutton(switch_frame, text='Middle Click',width=15,height=6,variable=switch_variable,indicatoron=False,value='middle_click')

#Used binding "<Button>" to bind the function with all mouse buttons
b1.bind("<Button>", hh)
b2.bind("<Button>", hh)
b3.bind("<Button>", hh)
b4.bind("<Button>", hh)



b1.grid(column=0, row=0)   
b2.grid(column=1, row=0)   
b3.grid(column=0, row=1)   
b4.grid(column=1, row=1)  

root.call('wm', 'attributes', '.', '-topmost', '1')
root.mainloop()

感谢您的帮助!

您可以通过“调用”被单击的 Radiobutton 小部件来实现,这是传递给偶数处理程序的 event 参数中包含的信息之一。

def hh(event,):
    print('This works as intended with *all* the mouse buttons.')
    event.widget.invoke()