newbie: Tkinter and Menu

Fredrik Lundh fredrik at pythonware.com
Tue Sep 21 18:07:08 EDT 1999


Charles Medcoff <cmedcoff at my-deja.com> wrote:
> Could someone post or reference a _simple_ example of how to create/use
> menu's in Tkinter.  I can't find any examples on line.

here's one:

#
# tkinter menus
#
# fredrik lundh, september 1999
#
# fredrik at pythonware.com
# http://www.pythonware.com
#

from Tkinter import *

root = Tk()
root.title("tk054 -- menu example")

separator = Frame(root, height=2, relief=SUNKEN, bd=2)
separator.pack(fill=X)

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

def callback():
    # in this snippet, all menu entries use the same callback...
    print "callback!"

menubar = Menu(root, tearoff=0)

filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Open...", command=callback)
filemenu.add_command(label="Save", command=callback)
filemenu.add_command(label="Save As...", command=callback)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)

helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="About", command=callback)

menubar.add_cascade(label="File", menu=filemenu)
menubar.add_cascade(label="Help", menu=helpmenu)

# attach it to the root window (also works for Toplevels)
root.config(menu=menubar)

mainloop()

</F>






More information about the Python-list mailing list