Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 1 | from 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 | |
| 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 | # +---------------+ |
| 33 | |
| 34 | |
| 35 | |
| 36 | def new_file(): |
| 37 | print "opening new file" |
| 38 | |
| 39 | |
| 40 | def open_file(): |
| 41 | print "opening OLD file" |
| 42 | |
| 43 | |
| 44 | def makeFileMenu(): |
| 45 | # make menu button : "File" |
| 46 | File_button = Menubutton(mBar, {'text': 'File', |
| 47 | 'underline': 0, |
| 48 | Pack: {'side': 'left', |
| 49 | 'padx': '1m'}}) |
| 50 | |
| 51 | # make the pulldown part of the File menu. The parameter passed is the master. |
| 52 | # we attach it to the File button as a python attribute called "menu" by convention. |
| 53 | # hopefully this isn't too confusing... |
| 54 | File_button.menu = Menu(File_button) |
| 55 | |
| 56 | # add an item. The first param is a menu entry type, |
| 57 | # must be one of: "cascade", "checkbutton", "command", "radiobutton", "seperator" |
| 58 | # see menu-demo-2.py for examples of use |
| 59 | File_button.menu.add('command', {'label': 'New...', |
| 60 | 'underline': 0, |
| 61 | 'command' : new_file}) |
| 62 | |
| 63 | |
| 64 | File_button.menu.add('command', {'label': 'Open...', |
| 65 | 'underline': 0, |
| 66 | 'command' : open_file}) |
| 67 | |
| 68 | File_button.menu.add('command', {'label': 'Quit', |
| 69 | 'underline': 0, |
| 70 | 'command': 'exit'}) |
| 71 | |
| 72 | |
| 73 | # set up a pointer from the file menubutton back to the file menu |
| 74 | File_button['menu'] = File_button.menu |
| 75 | |
| 76 | return File_button |
| 77 | |
| 78 | |
| 79 | |
| 80 | def makeEditMenu(): |
| 81 | Edit_button = Menubutton(mBar, {'text': 'Edit', |
| 82 | 'underline': 0, |
| 83 | Pack: {'side': 'left', |
| 84 | 'padx' : '1m'}}) |
| 85 | Edit_button.menu = Menu(Edit_button) |
| 86 | |
| 87 | # just to be cute, let's disable the undo option: |
| 88 | Edit_button.menu.add('command', {"label" : "Undo"} ) |
| 89 | # undo is the 0th entry... |
| 90 | Edit_button.menu.entryconfig(0, {"state" : "disabled"}) |
| 91 | |
| 92 | # and these are just for show. No "command" callbacks attached. |
| 93 | Edit_button.menu.add('command', {"label" : "Cut"} ) |
| 94 | Edit_button.menu.add('command', {"label" : "Copy"} ) |
| 95 | Edit_button.menu.add('command', {"label" : "Paste"} ) |
| 96 | |
| 97 | # set up a pointer from the file menubutton back to the file menu |
| 98 | Edit_button['menu'] = Edit_button.menu |
| 99 | |
| 100 | return Edit_button |
| 101 | |
| 102 | |
| 103 | ################################################# |
| 104 | |
| 105 | #### Main starts here ... |
| 106 | root = Tk() |
| 107 | |
| 108 | |
| 109 | # make a menu bar |
| 110 | mBar = Frame(root, {'relief': 'raised', |
| 111 | 'bd': 2, |
| 112 | Pack: {'side': 'top', |
| 113 | 'fill': 'x'}}) |
| 114 | |
| 115 | File_button = makeFileMenu() |
| 116 | Edit_button = makeEditMenu() |
| 117 | |
| 118 | # finally, install the buttons in the menu bar. |
| 119 | # This allows for scanning from one menubutton to the next. |
| 120 | mBar.tk_menuBar(File_button, Edit_button) |
| 121 | |
| 122 | |
| 123 | root.title('menu demo') |
| 124 | root.iconname('packer') |
| 125 | |
| 126 | root.mainloop() |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |