from tkinter import *
from tkinter import messagebox
class TestWin(Tk):
def __init__(self):
super().__init__()
self.title("右击,弹出选择菜单")
self.geometry("600x400+200+200")
self.bind("<Button-3>",self.RightClicked)
'''创建一个弹出菜单'''
self.menu = Menu(self,
tearoff=False,
)
self.menu.add_command(label="callback1", command=self.callback1)
self.menu.add_command(label="callback2", command=self.callback2)
self.menu.add_command(label="callback3", command=self.callback3)
def RightClicked(self,event):
self.menu.post(event.x_root, event.y_root)
def callback1(self,event=None):
messagebox.showinfo("Message", "callback1")
def callback2(event=None):
messagebox.showinfo("Message", "callback2")
def callback3(event=None):
messagebox.showinfo("Message", "callback3")
if __name__ == '__main__':
win = TestWin()
win.mainloop()