可以方便写个函数:
def showPopoutMenu(w, menu):
def popout(event):
menu.post(event.x + w.winfo_rootx(), event.y + w.winfo_rooty())
w.update()
w.bind('<Button-3>', popout)
应用:
1、用于整个窗口:
from tkinter import *
# Python 2.X 可以用from Tkinter import *
def showPopoutMenu(w, menu):
def popout(event):
menu.post(event.x + w.winfo_rootx(), event.y + w.winfo_rooty())
w.update()
w.bind('<Button-3>', popout)
w = Tk()
w.title('Pop-Out Menu')
menu = Menu(w)
menu.add_cascade(label = '功能一')
menu.add_cascade(label = '功能二')
showPopoutMenu(w, menu)
w.mainloop()
效果:
2、用于控件
from tkinter import *
# Python 2.X 可以用from Tkinter import *
def showPopoutMenu(w, menu):
def popout(event):
menu.post(event.x + w.winfo_rootx(), event.y + w.winfo_rooty())
w.update()
w.bind('<Button-3>', popout)
w = Tk()
w.title('Pop-Out Menu')
lab = Label(text = 'I am a Label widget with right-click menu! ')
lab.place(x = 0, y = 0)
menu = Menu(w)
menu.add_cascade(label = '功能一')
menu.add_cascade(label = '功能二')
showPopoutMenu(lab, menu)
w.mainloop()
效果:
?
|