首页 > 代码库 > pythonGUI菜单栏和弹出菜单

pythonGUI菜单栏和弹出菜单

菜单栏代码:

from  tkinter import *

root = Tk()

menubar = Menu(root)

def callback():
    pass
filemenu = Menu(menubar, tearoff=False)
filemenu.add_command(label="打开",command=callback)
filemenu.add_command(label="保存",command=callback)
filemenu.add_separator()
filemenu.add_command(label="退出",command=callback)
menubar.add_cascade(label="文件",menu=filemenu)

editmenu = Menu(menubar, tearoff=False)
editmenu.add_command(label="复制",command=callback)
editmenu.add_command(label="粘贴",command=callback)
editmenu.add_separator()
editmenu.add_command(label="退出",command=callback)
menubar.add_cascade(label="编辑",menu=editmenu)

root.config(menu=menubar)
mainloop()

运行截图:

技术分享

弹出菜单代码:

from tkinter import *

root = Tk()

def callback():
    pass6
menubar = Menu(root)
menubar.add_command(label="撤销",command=callback)
menubar.add_command(label="重置",command=callback)
menubar.add_command(label="复制",command=callback)

frame = Frame(root,width=512,height=512)
frame.pack()

def popup(event):
    menubar.post(event.x_root,event.y_root)

frame.bind("<Button-3>",popup)
mainloop()

 

pythonGUI菜单栏和弹出菜单