menu-all-types-of-entries.py 8.61 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
from Tkinter import *

# some vocabulary to keep from getting confused. This terminology 
# is something I cooked up for this file, but follows the man pages 
# pretty closely
# 
# 
# 
#       This is a MENUBUTTON
#       V
# +-------------+
# |             |
# 
# +------------++------------++------------+
# |            ||            ||            |
# |  File      ||  Edit      || Options    |   <-------- the MENUBAR
# |            ||            ||            |
# +------------++------------++------------+
# | New...         |
# | Open...        |
# | Print          |
# |                |  <-------- This is a MENU. The lines of text in the menu are
# |                |                            MENU ENTRIES
# |                +---------------+
# | Open Files >   | file1         |               
# |                | file2         |
# |                | another file  | <------ this cascading part is also a MENU
# +----------------|               |
#                  |               |
#                  |               |
#                  |               |
#                  +---------------+



# some miscellaneous callbacks
def new_file():
    print "opening new file"

def open_file():
    print "opening OLD file"

def print_something():
    print "picked a menu item"



anchovies = 0

def print_anchovies():
    global anchovies
    anchovies = not anchovies
    print "anchovies?", anchovies

def makeCommandMenu():
    # make menu button 
57 58 59
    Command_button = Menubutton(mBar, text='Simple Button Commands', 
				underline=0)
    Command_button.pack(side=LEFT, padx="2m")
Guido van Rossum's avatar
Guido van Rossum committed
60 61 62 63 64 65 66
    
    # make the pulldown part of the File menu. The parameter passed is the master.
    # we attach it to the button as a python attribute called "menu" by convention.
    # hopefully this isn't too confusing...
    Command_button.menu = Menu(Command_button)

    # just to be cute, let's disable the undo option:
67
    Command_button.menu.add_command(label="Undo")
Guido van Rossum's avatar
Guido van Rossum committed
68
    # undo is the 0th entry...
69 70 71 72 73 74 75 76 77
    Command_button.menu.entryconfig(0, state=DISABLED)

    Command_button.menu.add_command(label='New...', underline=0, 
				    command=new_file)
    Command_button.menu.add_command(label='Open...', underline=0, 
				    command=open_file)
    Command_button.menu.add_command(label='Different Font', underline=0,
				    font='-*-helvetica-*-r-*-*-*-180-*-*-*-*-*-*',
				    command=print_something)
Guido van Rossum's avatar
Guido van Rossum committed
78 79 80
    
    # we can make bitmaps be menu entries too. File format is X11 bitmap.
    # if you use XV, save it under X11 bitmap format. duh-uh.,..
81 82 83
    Command_button.menu.add_command(
	bitmap="info")
	#bitmap='@/home/mjc4y/dilbert/project.status.is.doomed.last.panel.bm')
Guido van Rossum's avatar
Guido van Rossum committed
84 85 86 87 88
    
    # this is just a line
    Command_button.menu.add('separator')

    # change the color
89 90 91 92 93
    Command_button.menu.add_command(label='Quit', underline=0, 
				    background='red', 
				    activebackground='green', 
				    command=Command_button.quit)

Guido van Rossum's avatar
Guido van Rossum committed
94 95 96 97 98 99 100 101 102
    # set up a pointer from the file menubutton back to the file menu
    Command_button['menu'] = Command_button.menu

    return Command_button



def makeCascadeMenu():
    # make menu button 
103 104
    Cascade_button = Menubutton(mBar, text='Cascading Menus', underline=0)
    Cascade_button.pack(side=LEFT, padx="2m")
Guido van Rossum's avatar
Guido van Rossum committed
105 106 107 108 109 110 111 112 113 114 115
    
    # the primary pulldown
    Cascade_button.menu = Menu(Cascade_button)

    # this is the menu that cascades from the primary pulldown....
    Cascade_button.menu.choices = Menu(Cascade_button.menu)

    # ...and this is a menu that cascades from that.
    Cascade_button.menu.choices.wierdones = Menu(Cascade_button.menu.choices)

    # then you define the menus from the deepest level on up.
116 117 118
    Cascade_button.menu.choices.wierdones.add_command(label='avacado')
    Cascade_button.menu.choices.wierdones.add_command(label='belgian endive')
    Cascade_button.menu.choices.wierdones.add_command(label='beefaroni')
Guido van Rossum's avatar
Guido van Rossum committed
119 120

    # definition of the menu one level up...
121 122 123 124 125 126 127 128 129
    Cascade_button.menu.choices.add_command(label='Chocolate')
    Cascade_button.menu.choices.add_command(label='Vanilla')
    Cascade_button.menu.choices.add_command(label='TuttiFruiti')
    Cascade_button.menu.choices.add_command(label='WopBopaLoopBapABopBamBoom')
    Cascade_button.menu.choices.add_command(label='Rocky Road')
    Cascade_button.menu.choices.add_command(label='BubbleGum')
    Cascade_button.menu.choices.add_cascade(
	label='Wierd Flavors', 
	menu=Cascade_button.menu.choices.wierdones)
Guido van Rossum's avatar
Guido van Rossum committed
130 131

    # and finally, the definition for the top level
132 133
    Cascade_button.menu.add_cascade(label='more choices', 
				    menu=Cascade_button.menu.choices)
Guido van Rossum's avatar
Guido van Rossum committed
134 135 136 137 138 139 140 141

    Cascade_button['menu'] = Cascade_button.menu

    return Cascade_button

def makeCheckbuttonMenu():
    global fred
    # make menu button 
142 143 144
    Checkbutton_button = Menubutton(mBar, text='Checkbutton Menus', 
				    underline=0)
    Checkbutton_button.pack(side=LEFT, padx='2m')
Guido van Rossum's avatar
Guido van Rossum committed
145 146 147 148 149 150 151
    
    # the primary pulldown
    Checkbutton_button.menu = Menu(Checkbutton_button)

    # and all the check buttons. Note that the "variable" "onvalue" and "offvalue" options
    # are not supported correctly at present. You have to do all your application 
    # work through the calback.
152 153 154
    Checkbutton_button.menu.add_checkbutton(label='Pepperoni')
    Checkbutton_button.menu.add_checkbutton(label='Sausage')
    Checkbutton_button.menu.add_checkbutton(label='Extra Cheese')
Guido van Rossum's avatar
Guido van Rossum committed
155 156

    # so here's a callback
157 158
    Checkbutton_button.menu.add_checkbutton(label='Anchovy', 
					    command=print_anchovies)
Guido van Rossum's avatar
Guido van Rossum committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

    # and start with anchovies selected to be on. Do this by 
    # calling invoke on this menu option. To refer to the "anchovy" menu
    # entry we need to know it's index. To do this, we use the index method
    # which takes arguments of several forms: 
    #
    # argument        what it does
    # -----------------------------------
    # a number        -- this is useless. 
    # "last"          -- last option in the menu
    # "none"          -- used with the activate command. see the man page on menus
    # "active"        -- the currently active menu option. A menu option is made active
    #                         with the 'activate' method
    # "@number"       -- where 'number' is an integer and is treated like a y coordinate in pixels
    # string pattern  -- this is the option used below, and attempts to match "labels" using the 
    #                    rules of Tcl_StringMatch
    Checkbutton_button.menu.invoke(Checkbutton_button.menu.index('Anchovy'))

    # set up a pointer from the file menubutton back to the file menu
    Checkbutton_button['menu'] = Checkbutton_button.menu

    return Checkbutton_button


def makeRadiobuttonMenu():
    # make menu button 
185 186 187
    Radiobutton_button = Menubutton(mBar, text='Radiobutton Menus', 
				    underline=0)
    Radiobutton_button.pack(side=LEFT, padx='2m')
Guido van Rossum's avatar
Guido van Rossum committed
188 189 190 191 192 193 194
    
    # the primary pulldown
    Radiobutton_button.menu = Menu(Radiobutton_button)

    # and all the Radio buttons. Note that the "variable" "onvalue" and "offvalue" options
    # are not supported correctly at present. You have to do all your application 
    # work through the calback.
195 196 197 198 199 200 201 202 203 204
    Radiobutton_button.menu.add_radiobutton(label='Republican')
    Radiobutton_button.menu.add_radiobutton(label='Democrat')
    Radiobutton_button.menu.add_radiobutton(label='Libertarian')
    Radiobutton_button.menu.add_radiobutton(label='Commie')
    Radiobutton_button.menu.add_radiobutton(label='Facist')
    Radiobutton_button.menu.add_radiobutton(label='Labor Party')
    Radiobutton_button.menu.add_radiobutton(label='Torie')
    Radiobutton_button.menu.add_radiobutton(label='Independent')
    Radiobutton_button.menu.add_radiobutton(label='Anarchist')
    Radiobutton_button.menu.add_radiobutton(label='No Opinion')
Guido van Rossum's avatar
Guido van Rossum committed
205 206 207 208 209 210 211 212

    # set up a pointer from the file menubutton back to the file menu
    Radiobutton_button['menu'] = Radiobutton_button.menu

    return Radiobutton_button


def makeDisabledMenu(): 
213 214
    Dummy_button = Menubutton(mBar, text='Dead Menu', underline=0)
    Dummy_button.pack(side=LEFT, padx='2m')
Guido van Rossum's avatar
Guido van Rossum committed
215 216

    # this is the standard way of turning off a whole menu
217
    Dummy_button["state"] = DISABLED
Guido van Rossum's avatar
Guido van Rossum committed
218 219
    return Dummy_button

220

Guido van Rossum's avatar
Guido van Rossum committed
221 222 223 224 225 226
#################################################
#### Main starts here ...
root = Tk()


# make a menu bar
227 228
mBar = Frame(root, relief=RAISED, borderwidth=2)
mBar.pack(fill=X)
Guido van Rossum's avatar
Guido van Rossum committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

Command_button     = makeCommandMenu()
Cascade_button     = makeCascadeMenu()
Checkbutton_button = makeCheckbuttonMenu()
Radiobutton_button = makeRadiobuttonMenu()
NoMenu             = makeDisabledMenu()

# finally, install the buttons in the menu bar. 
# This allows for scanning from one menubutton to the next.
mBar.tk_menuBar(Command_button, Cascade_button, Checkbutton_button, Radiobutton_button, NoMenu)


root.title('menu demo')
root.iconname('menu demo')

root.mainloop()