Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 1 | from Tkinter import * |
| 2 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 3 | # some vocabulary to keep from getting confused. This terminology |
| 4 | # is something I cooked up for this file, but follows the man pages |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 5 | # pretty closely |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 6 | # |
| 7 | # |
| 8 | # |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 9 | # This is a MENUBUTTON |
| 10 | # V |
| 11 | # +-------------+ |
| 12 | # | | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 13 | # |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 14 | # +------------++------------++------------+ |
| 15 | # | || || | |
| 16 | # | File || Edit || Options | <-------- the MENUBAR |
| 17 | # | || || | |
| 18 | # +------------++------------++------------+ |
| 19 | # | New... | |
| 20 | # | Open... | |
| 21 | # | Print | |
Guido van Rossum | b8fe9b3 | 1995-01-10 17:07:40 +0000 | [diff] [blame] | 22 | # | | <------ This is a MENU. The lines of text in the menu are |
| 23 | # | | MENU ENTRIES |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 24 | # | +---------------+ |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 25 | # | Open Files > | file1 | |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 26 | # | | file2 | |
| 27 | # | | another file | <------ this cascading part is also a MENU |
| 28 | # +----------------| | |
| 29 | # | | |
| 30 | # | | |
| 31 | # | | |
| 32 | # +---------------+ |
| 33 | |
| 34 | |
| 35 | |
| 36 | def new_file(): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 37 | print("opening new file") |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def open_file(): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 41 | print("opening OLD file") |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | def makeFileMenu(): |
| 45 | # make menu button : "File" |
Guido van Rossum | 89cb67b | 1996-07-30 18:57:18 +0000 | [diff] [blame] | 46 | File_button = Menubutton(mBar, text='File', underline=0) |
| 47 | File_button.pack(side=LEFT, padx="1m") |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 48 | File_button.menu = Menu(File_button) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 49 | |
| 50 | # add an item. The first param is a menu entry type, |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 51 | # must be one of: "cascade", "checkbutton", "command", "radiobutton", "seperator" |
| 52 | # see menu-demo-2.py for examples of use |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 53 | File_button.menu.add_command(label='New...', underline=0, |
| 54 | command=new_file) |
| 55 | |
| 56 | |
| 57 | File_button.menu.add_command(label='Open...', underline=0, |
| 58 | command=open_file) |
| 59 | |
| 60 | File_button.menu.add_command(label='Quit', underline=0, |
| 61 | command='exit') |
Guido van Rossum | 89cb67b | 1996-07-30 18:57:18 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 63 | # 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 | |
| 70 | def makeEditMenu(): |
Guido van Rossum | 89cb67b | 1996-07-30 18:57:18 +0000 | [diff] [blame] | 71 | Edit_button = Menubutton(mBar, text='Edit', underline=0) |
| 72 | Edit_button.pack(side=LEFT, padx="1m") |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 73 | Edit_button.menu = Menu(Edit_button) |
| 74 | |
| 75 | # just to be cute, let's disable the undo option: |
Guido van Rossum | 89cb67b | 1996-07-30 18:57:18 +0000 | [diff] [blame] | 76 | Edit_button.menu.add('command', label="Undo") |
Guido van Rossum | a4ddb23 | 1996-08-21 20:13:08 +0000 | [diff] [blame] | 77 | # Since the tear-off bar is the 0th entry, |
| 78 | # undo is the 1st entry... |
| 79 | Edit_button.menu.entryconfig(1, state=DISABLED) |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 80 | |
| 81 | # and these are just for show. No "command" callbacks attached. |
Guido van Rossum | 89cb67b | 1996-07-30 18:57:18 +0000 | [diff] [blame] | 82 | Edit_button.menu.add_command(label="Cut") |
| 83 | Edit_button.menu.add_command(label="Copy") |
| 84 | Edit_button.menu.add_command(label="Paste") |
| 85 | |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 86 | # set up a pointer from the file menubutton back to the file menu |
| 87 | Edit_button['menu'] = Edit_button.menu |
| 88 | |
| 89 | return Edit_button |
| 90 | |
| 91 | |
| 92 | ################################################# |
| 93 | |
| 94 | #### Main starts here ... |
| 95 | root = Tk() |
| 96 | |
| 97 | |
| 98 | # make a menu bar |
Guido van Rossum | 89cb67b | 1996-07-30 18:57:18 +0000 | [diff] [blame] | 99 | mBar = Frame(root, relief=RAISED, borderwidth=2) |
| 100 | mBar.pack(fill=X) |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 101 | |
| 102 | File_button = makeFileMenu() |
| 103 | Edit_button = makeEditMenu() |
| 104 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 105 | # finally, install the buttons in the menu bar. |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 106 | # This allows for scanning from one menubutton to the next. |
| 107 | mBar.tk_menuBar(File_button, Edit_button) |
| 108 | |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 109 | root.title('menu demo') |
| 110 | root.iconname('packer') |
| 111 | |
| 112 | root.mainloop() |