Martin v. Löwis | 8ec03e0 | 2002-03-17 18:19:13 +0000 | [diff] [blame] | 1 | # -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*- |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 2 | # |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 3 | # $Id$ |
Martin v. Löwis | 8ec03e0 | 2002-03-17 18:19:13 +0000 | [diff] [blame] | 4 | # |
| 5 | # Tix Demostration Program |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 6 | # |
| 7 | # This sample program is structured in such a way so that it can be |
Martin v. Löwis | 8ec03e0 | 2002-03-17 18:19:13 +0000 | [diff] [blame] | 8 | # executed from the Tix demo program "tixwidgets.py": it must have a |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 9 | # procedure called "RunSample". It should also have the "if" statment |
| 10 | # at the end of this file so that it can be run as a standalone |
| 11 | # program using tixwish. |
| 12 | |
| 13 | # This file demonstrates the use of the tixPopupMenu widget. |
| 14 | # |
Benjamin Peterson | d6d63f5 | 2009-01-04 18:53:28 +0000 | [diff] [blame] | 15 | import tkinter.tix |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 16 | |
| 17 | def RunSample(w): |
| 18 | # We create the frame and the button, then we'll bind the PopupMenu |
| 19 | # to both widgets. The result is, when you press the right mouse |
| 20 | # button over $w.top or $w.top.but, the PopupMenu will come up. |
| 21 | # |
Benjamin Peterson | d6d63f5 | 2009-01-04 18:53:28 +0000 | [diff] [blame] | 22 | top = tkinter.tix.Frame(w, relief=tkinter.tix.RAISED, bd=1) |
| 23 | but = tkinter.tix.Button(top, text='Press the right mouse button over this button or its surrounding area') |
| 24 | but.pack(expand=1, fill=tkinter.tix.BOTH, padx=50, pady=50) |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 25 | |
Benjamin Peterson | d6d63f5 | 2009-01-04 18:53:28 +0000 | [diff] [blame] | 26 | p = tkinter.tix.PopupMenu(top, title='Popup Test') |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 27 | p.bind_widget(top) |
| 28 | p.bind_widget(but) |
| 29 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 30 | # Set the entries inside the PopupMenu widget. |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 31 | # [Hint] You have to manipulate the "menu" subwidget. |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 32 | # $w.top.p itself is NOT a menu widget. |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 33 | # [Hint] Watch carefully how the sub-menu is created |
| 34 | # |
| 35 | p.menu.add_command(label='Desktop', underline=0) |
| 36 | p.menu.add_command(label='Select', underline=0) |
| 37 | p.menu.add_command(label='Find', underline=0) |
| 38 | p.menu.add_command(label='System', underline=1) |
| 39 | p.menu.add_command(label='Help', underline=0) |
Benjamin Peterson | d6d63f5 | 2009-01-04 18:53:28 +0000 | [diff] [blame] | 40 | m1 = tkinter.tix.Menu(p.menu) |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 41 | m1.add_command(label='Hello') |
| 42 | p.menu.add_cascade(label='More', menu=m1) |
| 43 | |
Benjamin Peterson | d6d63f5 | 2009-01-04 18:53:28 +0000 | [diff] [blame] | 44 | but.pack(side=tkinter.tix.TOP, padx=40, pady=50) |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 45 | |
Benjamin Peterson | d6d63f5 | 2009-01-04 18:53:28 +0000 | [diff] [blame] | 46 | box = tkinter.tix.ButtonBox(w, orientation=tkinter.tix.HORIZONTAL) |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 47 | box.add('ok', text='Ok', underline=0, width=6, |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 48 | command=lambda w=w: w.destroy()) |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 49 | box.add('cancel', text='Cancel', underline=0, width=6, |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 50 | command=lambda w=w: w.destroy()) |
Benjamin Peterson | d6d63f5 | 2009-01-04 18:53:28 +0000 | [diff] [blame] | 51 | box.pack(side=tkinter.tix.BOTTOM, fill=tkinter.tix.X) |
| 52 | top.pack(side=tkinter.tix.TOP, fill=tkinter.tix.BOTH, expand=1) |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 53 | |
| 54 | if __name__ == '__main__': |
Benjamin Peterson | d6d63f5 | 2009-01-04 18:53:28 +0000 | [diff] [blame] | 55 | root = tkinter.tix.Tk() |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 56 | RunSample(root) |
| 57 | root.mainloop() |