Don't reference removed files in Makefile
[python/dscho.git] / Demo / tkinter / matt / menu-all-types-of-entries.py
blob5341ca2b6f3fb1a5909fd1ece38ae258b4af69ab
1 from Tkinter import *
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 # | |
14 # +------------++------------++------------+
15 # | || || |
16 # | File || Edit || Options | <-------- the MENUBAR
17 # | || || |
18 # +------------++------------++------------+
19 # | New... |
20 # | Open... |
21 # | Print |
22 # | | <-------- This is a MENU. The lines of text in the menu are
23 # | | MENU ENTRIES
24 # | +---------------+
25 # | Open Files > | file1 |
26 # | | file2 |
27 # | | another file | <------ this cascading part is also a MENU
28 # +----------------| |
29 # | |
30 # | |
31 # | |
32 # +---------------+
36 # some miscellaneous callbacks
37 def new_file():
38 print "opening new file"
41 def open_file():
42 print "opening OLD file"
44 def print_something():
45 print "picked a menu item"
49 anchovies = 0
51 def print_anchovies():
52 global anchovies
53 anchovies = not anchovies
54 print "anchovies?", anchovies
56 def makeCommandMenu():
57 # make menu button
58 Command_button = Menubutton(mBar, {'text': 'Simple Button Commands',
59 'underline': 0,
60 Pack: {'side': 'left',
61 'padx': '2m'}})
63 # make the pulldown part of the File menu. The parameter passed is the master.
64 # we attach it to the button as a python attribute called "menu" by convention.
65 # hopefully this isn't too confusing...
66 Command_button.menu = Menu(Command_button)
69 # just to be cute, let's disable the undo option:
70 Command_button.menu.add('command', {"label" : "Undo"} )
71 # undo is the 0th entry...
72 Command_button.menu.entryconfig(0, {"state" : "disabled"})
74 Command_button.menu.add('command', {'label': 'New...',
76 'underline': 0,
77 'command' : new_file})
80 Command_button.menu.add('command', {'label': 'Open...',
81 'underline': 0,
82 'command' : open_file})
84 Command_button.menu.add('command', {'label': 'Different Font',
85 'underline': 0,
86 'font' : '-*-helvetica-*-r-*-*-*-180-*-*-*-*-*-*',
87 'command' : print_something})
89 # we can make bitmaps be menu entries too. File format is X11 bitmap.
90 # if you use XV, save it under X11 bitmap format. duh-uh.,..
91 # Command_button.menu.add('command', {'bitmap' : '@/home/mjc4y/ftp/tcl/tk3.6/library/demos/bitmaps/face'})
92 Command_button.menu.add('command', {'bitmap' : '@/home/mjc4y/dilbert/project.status.is.doomed.last.panel.bm'})
94 # this is just a line
95 Command_button.menu.add('separator')
97 # change the color
98 Command_button.menu.add('command', {'label': 'Quit',
99 'underline': 0,
100 'background' : 'red',
101 'activebackground' : 'green',
102 'command': 'exit'})
105 # set up a pointer from the file menubutton back to the file menu
106 Command_button['menu'] = Command_button.menu
108 return Command_button
112 def makeCascadeMenu():
113 # make menu button
114 Cascade_button = Menubutton(mBar, {'text': 'Cascading Menus',
115 'underline': 0,
116 Pack: {'side': 'left',
117 'padx': '2m'}})
119 # the primary pulldown
120 Cascade_button.menu = Menu(Cascade_button)
122 # this is the menu that cascades from the primary pulldown....
123 Cascade_button.menu.choices = Menu(Cascade_button.menu)
125 # ...and this is a menu that cascades from that.
126 Cascade_button.menu.choices.wierdones = Menu(Cascade_button.menu.choices)
128 # then you define the menus from the deepest level on up.
129 Cascade_button.menu.choices.wierdones.add('command', {'label' : 'avacado'})
130 Cascade_button.menu.choices.wierdones.add('command', {'label' : 'belgian endive'})
131 Cascade_button.menu.choices.wierdones.add('command', {'label' : 'beefaroni'})
133 # definition of the menu one level up...
134 Cascade_button.menu.choices.add('command', {'label' : 'Chocolate'})
135 Cascade_button.menu.choices.add('command', {'label' : 'Vanilla'})
136 Cascade_button.menu.choices.add('command', {'label' : 'TuttiFruiti'})
137 Cascade_button.menu.choices.add('command', {'label' : 'WopBopaLoopBapABopBamBoom'})
138 Cascade_button.menu.choices.add('command', {'label' : 'Rocky Road'})
139 Cascade_button.menu.choices.add('command', {'label' : 'BubbleGum'})
140 Cascade_button.menu.choices.add('cascade', {'label' : 'Wierd Flavors',
141 'menu' : Cascade_button.menu.choices.wierdones})
143 # and finally, the definition for the top level
144 Cascade_button.menu.add('cascade', {'label' : 'more choices',
145 'menu' : Cascade_button.menu.choices})
148 Cascade_button['menu'] = Cascade_button.menu
150 return Cascade_button
152 def makeCheckbuttonMenu():
153 global fred
154 # make menu button
155 Checkbutton_button = Menubutton(mBar, {'text': 'Checkbutton Menus',
156 'underline': 0,
157 Pack: {'side': 'left',
158 'padx': '2m'}})
160 # the primary pulldown
161 Checkbutton_button.menu = Menu(Checkbutton_button)
163 # and all the check buttons. Note that the "variable" "onvalue" and "offvalue" options
164 # are not supported correctly at present. You have to do all your application
165 # work through the calback.
166 Checkbutton_button.menu.add('checkbutton', {'label': 'Pepperoni'})
167 Checkbutton_button.menu.add('checkbutton', {'label': 'Sausage'})
168 Checkbutton_button.menu.add('checkbutton', {'label': 'Extra Cheese'})
170 # so here's a callback
171 Checkbutton_button.menu.add('checkbutton', {'label': 'Anchovy',
172 'command' : print_anchovies})
174 # and start with anchovies selected to be on. Do this by
175 # calling invoke on this menu option. To refer to the "anchovy" menu
176 # entry we need to know it's index. To do this, we use the index method
177 # which takes arguments of several forms:
179 # argument what it does
180 # -----------------------------------
181 # a number -- this is useless.
182 # "last" -- last option in the menu
183 # "none" -- used with the activate command. see the man page on menus
184 # "active" -- the currently active menu option. A menu option is made active
185 # with the 'activate' method
186 # "@number" -- where 'number' is an integer and is treated like a y coordinate in pixels
187 # string pattern -- this is the option used below, and attempts to match "labels" using the
188 # rules of Tcl_StringMatch
189 Checkbutton_button.menu.invoke(Checkbutton_button.menu.index('Anchovy'))
191 # set up a pointer from the file menubutton back to the file menu
192 Checkbutton_button['menu'] = Checkbutton_button.menu
194 return Checkbutton_button
197 def makeRadiobuttonMenu():
198 # make menu button
199 Radiobutton_button = Menubutton(mBar, {'text': 'Radiobutton Menus',
200 'underline': 0,
201 Pack: {'side': 'left',
202 'padx': '2m'}})
204 # the primary pulldown
205 Radiobutton_button.menu = Menu(Radiobutton_button)
207 # and all the Radio buttons. Note that the "variable" "onvalue" and "offvalue" options
208 # are not supported correctly at present. You have to do all your application
209 # work through the calback.
210 Radiobutton_button.menu.add('radiobutton', {'label': 'Republican'})
211 Radiobutton_button.menu.add('radiobutton', {'label': 'Democrat'})
212 Radiobutton_button.menu.add('radiobutton', {'label': 'Libertarian'})
213 Radiobutton_button.menu.add('radiobutton', {'label': 'Commie'})
214 Radiobutton_button.menu.add('radiobutton', {'label': 'Facist'})
215 Radiobutton_button.menu.add('radiobutton', {'label': 'Labor Party'})
216 Radiobutton_button.menu.add('radiobutton', {'label': 'Torie'})
217 Radiobutton_button.menu.add('radiobutton', {'label': 'Independent'})
218 Radiobutton_button.menu.add('radiobutton', {'label': 'Anarchist'})
219 Radiobutton_button.menu.add('radiobutton', {'label': 'No Opinion'})
221 # set up a pointer from the file menubutton back to the file menu
222 Radiobutton_button['menu'] = Radiobutton_button.menu
224 return Radiobutton_button
227 def makeDisabledMenu():
228 Dummy_button = Menubutton(mBar, {'text': 'Dead Menu',
229 'underline': 0,
230 Pack: {'side': 'left',
231 'padx': '2m'}})
233 # this is the standard way of turning off a whole menu
234 Dummy_button["state"] = "disabled"
235 return Dummy_button
237 #################################################
238 #### Main starts here ...
239 root = Tk()
242 # make a menu bar
243 mBar = Frame(root, {'relief': 'raised',
244 'bd': 2,
245 Pack: {'side': 'top',
246 'fill': 'x'}})
248 Command_button = makeCommandMenu()
249 Cascade_button = makeCascadeMenu()
250 Checkbutton_button = makeCheckbuttonMenu()
251 Radiobutton_button = makeRadiobuttonMenu()
252 NoMenu = makeDisabledMenu()
254 # finally, install the buttons in the menu bar.
255 # This allows for scanning from one menubutton to the next.
256 mBar.tk_menuBar(Command_button, Cascade_button, Checkbutton_button, Radiobutton_button, NoMenu)
259 root.title('menu demo')
260 root.iconname('menu demo')
262 root.mainloop()