Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 1 | #!/usr/local/bin/python |
| 2 | # |
| 3 | # $Id$ |
| 4 | # |
| 5 | # Tix Demostration Program |
| 6 | # |
| 7 | # This sample program is structured in such a way so that it can be |
| 8 | # executed from the Tix demo program "tixwidgets": it must have a |
| 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. |
| 12 | |
| 13 | # This file demonstrates the use of the tixComboBox widget, which is close |
| 14 | # to the MS Window Combo Box control. |
| 15 | # |
| 16 | import Tix |
| 17 | |
| 18 | def RunSample(w): |
| 19 | global demo_month, demo_year |
| 20 | |
| 21 | top = Tix.Frame(w, bd=1, relief=Tix.RAISED) |
| 22 | |
| 23 | demo_month = Tix.StringVar() |
| 24 | demo_year = Tix.StringVar() |
| 25 | |
| 26 | # $w.top.a is a drop-down combo box. It is not editable -- who wants |
| 27 | # to invent new months? |
| 28 | # |
| 29 | # [Hint] The -options switch sets the options of the subwidgets. |
| 30 | # [Hint] We set the label.width subwidget option of both comboboxes to |
| 31 | # be 10 so that their labels appear to be aligned. |
| 32 | # |
| 33 | a = Tix.ComboBox(top, label="Month: ", dropdown=1, |
| 34 | command=select_month, editable=0, variable=demo_month, |
| 35 | options='listbox.height 6 label.width 10 label.anchor e') |
| 36 | |
| 37 | # $w.top.b is a non-drop-down combo box. It is not editable: we provide |
| 38 | # four choices for the user, but he can enter an alternative year if he |
| 39 | # wants to. |
| 40 | # |
| 41 | # [Hint] Use the padY and anchor options of the label subwidget to |
| 42 | # align the label with the entry subwidget. |
| 43 | # [Hint] Notice that you should use padY (the NAME of the option) and not |
| 44 | # pady (the SWITCH of the option). |
| 45 | # |
| 46 | b = Tix.ComboBox(top, label="Year: ", dropdown=0, |
| 47 | command=select_year, editable=1, variable=demo_year, |
| 48 | options='listbox.height 4 label.padY 5 label.width 10 label.anchor ne') |
| 49 | |
| 50 | a.pack(side=Tix.TOP, anchor=Tix.W) |
| 51 | b.pack(side=Tix.TOP, anchor=Tix.W) |
| 52 | |
| 53 | a.insert(Tix.END, 'January') |
| 54 | a.insert(Tix.END, 'February') |
| 55 | a.insert(Tix.END, 'March') |
| 56 | a.insert(Tix.END, 'April') |
| 57 | a.insert(Tix.END, 'May') |
| 58 | a.insert(Tix.END, 'June') |
| 59 | a.insert(Tix.END, 'July') |
| 60 | a.insert(Tix.END, 'August') |
| 61 | a.insert(Tix.END, 'September') |
| 62 | a.insert(Tix.END, 'October') |
| 63 | a.insert(Tix.END, 'November') |
| 64 | a.insert(Tix.END, 'December') |
| 65 | |
| 66 | b.insert(Tix.END, '1992') |
| 67 | b.insert(Tix.END, '1993') |
| 68 | b.insert(Tix.END, '1994') |
| 69 | b.insert(Tix.END, '1995') |
| 70 | b.insert(Tix.END, '1996') |
| 71 | |
| 72 | # Use "tixSetSilent" to set the values of the combo box if you |
| 73 | # don't want your -command procedures (cbx:select_month and |
| 74 | # cbx:select_year) to be called. |
| 75 | # |
| 76 | a.set_silent('January') |
| 77 | b.set_silent('1995') |
| 78 | |
| 79 | box = Tix.ButtonBox(w, orientation=Tix.HORIZONTAL) |
| 80 | box.add('ok', text='Ok', underline=0, width=6, |
| 81 | command=lambda w=w: ok_command(w)) |
| 82 | box.add('cancel', text='Cancel', underline=0, width=6, |
| 83 | command=lambda w=w: w.destroy()) |
| 84 | box.pack(side=Tix.BOTTOM, fill=Tix.X) |
| 85 | top.pack(side=Tix.TOP, fill=Tix.BOTH, expand=1) |
| 86 | |
| 87 | def select_month(event=None): |
| 88 | print "Month =", demo_month.get() |
| 89 | |
| 90 | def select_year(event=None): |
| 91 | print "Year =", demo_year.get() |
| 92 | |
| 93 | def ok_command(w): |
| 94 | print "Month =", demo_month.get(), ", Year=", demo_year.get() |
| 95 | w.destroy() |
| 96 | |
| 97 | if __name__ == '__main__': |
| 98 | root = Tix.Tk() |
| 99 | RunSample(root) |
| 100 | root.mainloop() |