python TKinter Popup Menu Instance Method
- 2021-12-09 09:13:12
- OfStack
1. Pop-up menus are also called context menus. Create menus and add various functions to menus.
2. Right-click to monitor the mouse. If right-click, it will pop up according to the position judgment.
3. Call the Menupop method.
4. add_separator Add a delimiter.
Instances
# Pop-up menu case
import tkinter
def makeLabel():
global baseFrame
tkinter.Label(baseFrame, text="PHP Is the best programming language, and I use Python").pack()
baseFrame = tkinter.Tk()
menubar = tkinter.Menu(baseFrame)
for x in [' Spicy mushroom ', ' Steam pot chicken ', ' Dongpo's elbow ']:
menubar.add_separator()
menubar.add_command(label=x)
menubar.add_command(label=" Chongqing hot pot ", command=makeLabel)
# Event handler 1 There must be at least one 1 Parameters, and the first 1 Parameters represent system events
def pop(event):
# Pay attention to the use event.x And event.x_root Difference between
# menubar.post(event.x, event.y)
menubar.post(event.x_root, event.y_root)
baseFrame.bind("<Button-3>", pop)
baseFrame.mainloop()
Instance extension:
from tkinter import *
def sys_callbak():
pass
def fun_callbak():
pass
def no_thing(event):
popmenu.post(event.x_root,event.y_root)
master = Tk()
master.title(' Automatic news crawling ')
menubar = Menu(master)
sysmenu = Menu(menubar,tearoff=False)
sysmenu.add_command(label=' Add ',command=sys_callbak)
sysmenu.add_command(label=' Modify ',command=sys_callbak)
sysmenu.add_separator()
sysmenu.add_command(label=' Quit ',command=sys_callbak)
menubar.add_cascade(label=' System ',menu=sysmenu)
funmenu = Menu(menubar)
funmenu.add_command(label=' Add ',command=fun_callbak)
funmenu.add_command(label=' Modify ',command=fun_callbak)
menubar.add_cascade(label=' Function ',menu=funmenu)
popmenu = Menu(master)
popmenu.add_command(label=' Not set ',command=no_thing)
popmenu.add_command(label=' The Beauty of Thinking ',command=no_thing)
frame = Frame(master,width=312,height=512)
frame.bind('<Button-3>',no_thing)
frame.grid()
master.grid()
master.config(menu=menubar)
mainloop()