python实例代码之实现文档接口菜单(打开文件\新建文件)

发布时间:2019-12-05编辑:脚本学堂
本文分享一例python代码,学习实现文档接口菜单的方法,教大家新建文件、打开文件的方法,有需要的朋友参考下吧。

python文档接口菜单的实现方法。
代码如下:

#!/bin/python
from Tkinter import *
from tkMessageBox import *

def notdone():
    showerror('Not implemented', 'Not yet available')

def makemenu(win):
    top = Menu(win)
    win.config(menu=top)

    file = Menu(top)
    file.add_command(label='New...',  command=notdone,  underline=0)
    file.add_command(label='Open...', command=notdone,  underline=0)
    file.add_command(label='Quit',    command=win.quit, underline=0)
    top.add_cascade(label='File',     menu=file,        underline=0)

    edit = Menu(top, tearoff=0)
    edit.add_command(label='Cut',     command=notdone,  underline=0)
    edit.add_command(label='Paste',   command=notdone,  underline=0)
    edit.add_separator()
    top.add_cascade(label='Edit',     menu=edit,        underline=0)

    submenu = Menu(edit, tearoff=0)
    submenu.add_command(label='A', command=win.quit, underline=0)
    submenu.add_command(label='B', command=notdone,  underline=0)
    edit.add_cascade(label='Stuff',   menu=submenu,     underline=0)

if __name__ == '__main__':
    root = Tk()
    root.title('menu_win')
    makemenu(root)
    msg = Label(root, text='Window menu basics')
    msg.pack(expand=YES, fill=BOTH)
    msg.config(relief=SUNKEN, width=40, height=7, bg='beige')
    root.mainloop()

效果图,如下:
python文档接口