blob: 16172dddda8dd30e7d5406ddebd88ab3dac98589 [file] [log] [blame]
Guido van Rossum35820f71994-10-07 09:55:26 +00001from Tkinter import *
2
3# some vocabulary to keep from getting confused. This terminology
4# is something I cooked up for this file, but follows the man pages
5# pretty closely
6#
7#
8#
9# This is a MENUBUTTON
10# V
11# +-------------+
12# | |
13#
14# +------------++------------++------------+
15# | || || |
16# | File || Edit || Options | <-------- the MENUBAR
17# | || || |
18# +------------++------------++------------+
19# | New... |
20# | Open... |
21# | Print |
Guido van Rossumb8fe9b31995-01-10 17:07:40 +000022# | | <------ This is a MENU. The lines of text in the menu are
23# | | MENU ENTRIES
Guido van Rossum35820f71994-10-07 09:55:26 +000024# | +---------------+
25# | Open Files > | file1 |
26# | | file2 |
27# | | another file | <------ this cascading part is also a MENU
28# +----------------| |
29# | |
30# | |
31# | |
32# +---------------+
33
34
35
36def new_file():
37 print "opening new file"
38
39
40def open_file():
41 print "opening OLD file"
42
43
44def makeFileMenu():
45 # make menu button : "File"
Guido van Rossum89cb67b1996-07-30 18:57:18 +000046 File_button = Menubutton(mBar, text='File', underline=0)
47 File_button.pack(side=LEFT, padx="1m")
Guido van Rossum35820f71994-10-07 09:55:26 +000048 File_button.menu = Menu(File_button)
49
50 # add an item. The first param is a menu entry type,
51 # must be one of: "cascade", "checkbutton", "command", "radiobutton", "seperator"
52 # see menu-demo-2.py for examples of use
Guido van Rossum89cb67b1996-07-30 18:57:18 +000053 File_button.menu.add_command(label='New...', underline=0,
54 command=new_file)
Guido van Rossum35820f71994-10-07 09:55:26 +000055
56
Guido van Rossum89cb67b1996-07-30 18:57:18 +000057 File_button.menu.add_command(label='Open...', underline=0,
58 command=open_file)
Guido van Rossum35820f71994-10-07 09:55:26 +000059
Guido van Rossum89cb67b1996-07-30 18:57:18 +000060 File_button.menu.add_command(label='Quit', underline=0,
61 command='exit')
62
Guido van Rossum35820f71994-10-07 09:55:26 +000063 # set up a pointer from the file menubutton back to the file menu
64 File_button['menu'] = File_button.menu
65
66 return File_button
67
68
69
70def makeEditMenu():
Guido van Rossum89cb67b1996-07-30 18:57:18 +000071 Edit_button = Menubutton(mBar, text='Edit', underline=0)
72 Edit_button.pack(side=LEFT, padx="1m")
Guido van Rossum35820f71994-10-07 09:55:26 +000073 Edit_button.menu = Menu(Edit_button)
74
75 # just to be cute, let's disable the undo option:
Guido van Rossum89cb67b1996-07-30 18:57:18 +000076 Edit_button.menu.add('command', label="Undo")
Guido van Rossum35820f71994-10-07 09:55:26 +000077 # undo is the 0th entry...
Guido van Rossum89cb67b1996-07-30 18:57:18 +000078 Edit_button.menu.entryconfig(0, state=DISABLED)
Guido van Rossum35820f71994-10-07 09:55:26 +000079
80 # and these are just for show. No "command" callbacks attached.
Guido van Rossum89cb67b1996-07-30 18:57:18 +000081 Edit_button.menu.add_command(label="Cut")
82 Edit_button.menu.add_command(label="Copy")
83 Edit_button.menu.add_command(label="Paste")
84
Guido van Rossum35820f71994-10-07 09:55:26 +000085 # set up a pointer from the file menubutton back to the file menu
86 Edit_button['menu'] = Edit_button.menu
87
88 return Edit_button
89
90
91#################################################
92
93#### Main starts here ...
94root = Tk()
95
96
97# make a menu bar
Guido van Rossum89cb67b1996-07-30 18:57:18 +000098mBar = Frame(root, relief=RAISED, borderwidth=2)
99mBar.pack(fill=X)
Guido van Rossum35820f71994-10-07 09:55:26 +0000100
101File_button = makeFileMenu()
102Edit_button = makeEditMenu()
103
104# finally, install the buttons in the menu bar.
105# This allows for scanning from one menubutton to the next.
106mBar.tk_menuBar(File_button, Edit_button)
107
Guido van Rossum35820f71994-10-07 09:55:26 +0000108root.title('menu demo')
109root.iconname('packer')
110
111root.mainloop()
112
113
114
115
116
117