menu-all-types-of-entries.py 8.9 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1 2
from Tkinter import *

3 4
# some vocabulary to keep from getting confused. This terminology
# is something I cooked up for this file, but follows the man pages
Guido van Rossum's avatar
Guido van Rossum committed
5
# pretty closely
6 7 8
#
#
#
Guido van Rossum's avatar
Guido van Rossum committed
9 10 11 12
#       This is a MENUBUTTON
#       V
# +-------------+
# |             |
13
#
Guido van Rossum's avatar
Guido van Rossum committed
14 15 16 17 18 19 20 21 22 23 24
# +------------++------------++------------+
# |            ||            ||            |
# |  File      ||  Edit      || Options    |   <-------- the MENUBAR
# |            ||            ||            |
# +------------++------------++------------+
# | New...         |
# | Open...        |
# | Print          |
# |                |  <-------- This is a MENU. The lines of text in the menu are
# |                |                            MENU ENTRIES
# |                +---------------+
25
# | Open Files >   | file1         |
Guido van Rossum's avatar
Guido van Rossum committed
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
# |                | 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():
56 57 58
    # make menu button
    Command_button = Menubutton(mBar, text='Simple Button Commands',
                                underline=0)
59
    Command_button.pack(side=LEFT, padx="2m")
60

Guido van Rossum's avatar
Guido van Rossum committed
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
    Command_button.menu.entryconfig(0, state=DISABLED)

71 72 73 74
    Command_button.menu.add_command(label='New...', underline=0,
                                    command=new_file)
    Command_button.menu.add_command(label='Open...', underline=0,
                                    command=open_file)
75
    Command_button.menu.add_command(label='Different Font', underline=0,
76 77 78
                                    font='-*-helvetica-*-r-*-*-*-180-*-*-*-*-*-*',
                                    command=print_something)

Guido van Rossum's avatar
Guido van Rossum committed
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
    Command_button.menu.add_command(
82 83 84
        bitmap="info")
        #bitmap='@/home/mjc4y/dilbert/project.status.is.doomed.last.panel.bm')

Guido van Rossum's avatar
Guido van Rossum committed
85 86 87 88
    # this is just a line
    Command_button.menu.add('separator')

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

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

    return Command_button



def makeCascadeMenu():
102
    # make menu button
103 104
    Cascade_button = Menubutton(mBar, text='Cascading Menus', underline=0)
    Cascade_button.pack(side=LEFT, padx="2m")
105

Guido van Rossum's avatar
Guido van Rossum committed
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
    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(
128 129
        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

    Cascade_button['menu'] = Cascade_button.menu

    return Cascade_button

def makeCheckbuttonMenu():
    global fred
141 142 143
    # make menu button
    Checkbutton_button = Menubutton(mBar, text='Checkbutton Menus',
                                    underline=0)
144
    Checkbutton_button.pack(side=LEFT, padx='2m')
145

Guido van Rossum's avatar
Guido van Rossum committed
146 147 148 149
    # the primary pulldown
    Checkbutton_button.menu = Menu(Checkbutton_button)

    # and all the check buttons. Note that the "variable" "onvalue" and "offvalue" options
150
    # are not supported correctly at present. You have to do all your application
Guido van Rossum's avatar
Guido van Rossum committed
151
    # 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
    # and start with anchovies selected to be on. Do this by
Guido van Rossum's avatar
Guido van Rossum committed
161 162
    # 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
163
    # which takes arguments of several forms:
Guido van Rossum's avatar
Guido van Rossum committed
164 165 166
    #
    # argument        what it does
    # -----------------------------------
167
    # a number        -- this is useless.
Guido van Rossum's avatar
Guido van Rossum committed
168 169 170 171 172
    # "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
173
    # string pattern  -- this is the option used below, and attempts to match "labels" using the
Guido van Rossum's avatar
Guido van Rossum committed
174 175 176 177 178 179 180 181 182 183
    #                    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():
184 185 186
    # make menu button
    Radiobutton_button = Menubutton(mBar, text='Radiobutton Menus',
                                    underline=0)
187
    Radiobutton_button.pack(side=LEFT, padx='2m')
188

Guido van Rossum's avatar
Guido van Rossum committed
189 190 191 192
    # the primary pulldown
    Radiobutton_button.menu = Menu(Radiobutton_button)

    # and all the Radio buttons. Note that the "variable" "onvalue" and "offvalue" options
193
    # are not supported correctly at present. You have to do all your application
Guido van Rossum's avatar
Guido van Rossum committed
194
    # 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

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

    return Radiobutton_button


212
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

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

236
# finally, install the buttons in the menu bar.
Guido van Rossum's avatar
Guido van Rossum committed
237 238 239 240 241 242 243 244
# 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()