[英]How to bind a function to a button from a right click menu in tkinter?
我正在 tkinter 中编写一个程序,我正在尝试将 function 绑定到右键单击菜单中的“删除”按钮。 我怎样才能做到这一点?
所以到目前为止我所做的是:
创建我的右键菜单:
self.right_click_menu = tk.Menu(self.mytab, tearoff=0)
创建一个 function 我想用我稍后添加的按钮“删除”来调用
def delete_plst(self, plst):
print('deleted')
我也有这个弹出 function 用于 tkinter 右键菜单
def do_popup(self, events, x):
self.right_click = x
self.right_click_menu.tk_popup(events.x_root, events.y_root)
self.right_click_menu.grab_release()
# if i uncomment the following line the delete_plst func will run anyways even if I right click my
# button and then not clicking 'remove'
# self.delete_plst(pslt=self.right_click)
然后我在我的右键菜单中添加一个命令
self.right_click_menu.add_command(label='Remove', command=lambda: self.delete_plst)
并将右键单击绑定到弹出窗口function:
mybutton.bind('<Button-3>', lambda events, x=i: self.do_popup(events, x=x))
基本上对我来说理想的是做这样的事情:
self.right_click_menu.bind('<MyRemoveButtonInsideTheRightClickMenu>', self.delete_plst)
如何仅在单击“删除”按钮而不是右键单击按钮时才调用删除 function?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.