blob: 80d78f2603e13547383c4ca90847486770bddba9 [file] [log] [blame]
Martin v. Löwis8ec03e02002-03-17 18:19:13 +00001# -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
Tim Peters182b5ac2004-07-18 06:16:08 +00002#
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +00003# $Id$
4#
5# Tix Demostration Program
6#
7# This sample program is structured in such a way so that it can be
Martin v. Löwis8ec03e02002-03-17 18:19:13 +00008# executed from the Tix demo program "tixwidgets.py": it must have a
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +00009# 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#
Benjamin Petersond6d63f52009-01-04 18:53:28 +000016import tkinter.tix
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000017
18def RunSample(w):
19 global demo_month, demo_year
20
Benjamin Petersond6d63f52009-01-04 18:53:28 +000021 top = tkinter.tix.Frame(w, bd=1, relief=tkinter.tix.RAISED)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000022
Benjamin Petersond6d63f52009-01-04 18:53:28 +000023 demo_month = tkinter.tix.StringVar()
24 demo_year = tkinter.tix.StringVar()
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000025
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.
Tim Peters182b5ac2004-07-18 06:16:08 +000030 # [Hint] We set the label.width subwidget option of both comboboxes to
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000031 # be 10 so that their labels appear to be aligned.
32 #
Benjamin Petersond6d63f52009-01-04 18:53:28 +000033 a = tkinter.tix.ComboBox(top, label="Month: ", dropdown=1,
Tim Peters182b5ac2004-07-18 06:16:08 +000034 command=select_month, editable=0, variable=demo_month,
35 options='listbox.height 6 label.width 10 label.anchor e')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000036
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
Tim Peters182b5ac2004-07-18 06:16:08 +000042 # align the label with the entry subwidget.
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000043 # [Hint] Notice that you should use padY (the NAME of the option) and not
44 # pady (the SWITCH of the option).
45 #
Benjamin Petersond6d63f52009-01-04 18:53:28 +000046 b = tkinter.tix.ComboBox(top, label="Year: ", dropdown=0,
Tim Peters182b5ac2004-07-18 06:16:08 +000047 command=select_year, editable=1, variable=demo_year,
48 options='listbox.height 4 label.padY 5 label.width 10 label.anchor ne')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000049
Benjamin Petersond6d63f52009-01-04 18:53:28 +000050 a.pack(side=tkinter.tix.TOP, anchor=tkinter.tix.W)
51 b.pack(side=tkinter.tix.TOP, anchor=tkinter.tix.W)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000052
Benjamin Petersond6d63f52009-01-04 18:53:28 +000053 a.insert(tkinter.tix.END, 'January')
54 a.insert(tkinter.tix.END, 'February')
55 a.insert(tkinter.tix.END, 'March')
56 a.insert(tkinter.tix.END, 'April')
57 a.insert(tkinter.tix.END, 'May')
58 a.insert(tkinter.tix.END, 'June')
59 a.insert(tkinter.tix.END, 'July')
60 a.insert(tkinter.tix.END, 'August')
61 a.insert(tkinter.tix.END, 'September')
62 a.insert(tkinter.tix.END, 'October')
63 a.insert(tkinter.tix.END, 'November')
64 a.insert(tkinter.tix.END, 'December')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000065
Benjamin Petersond6d63f52009-01-04 18:53:28 +000066 b.insert(tkinter.tix.END, '1992')
67 b.insert(tkinter.tix.END, '1993')
68 b.insert(tkinter.tix.END, '1994')
69 b.insert(tkinter.tix.END, '1995')
70 b.insert(tkinter.tix.END, '1996')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000071
72 # Use "tixSetSilent" to set the values of the combo box if you
Tim Peters182b5ac2004-07-18 06:16:08 +000073 # don't want your -command procedures (cbx:select_month and
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000074 # cbx:select_year) to be called.
75 #
76 a.set_silent('January')
77 b.set_silent('1995')
78
Benjamin Petersond6d63f52009-01-04 18:53:28 +000079 box = tkinter.tix.ButtonBox(w, orientation=tkinter.tix.HORIZONTAL)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000080 box.add('ok', text='Ok', underline=0, width=6,
Tim Peters182b5ac2004-07-18 06:16:08 +000081 command=lambda w=w: ok_command(w))
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000082 box.add('cancel', text='Cancel', underline=0, width=6,
Tim Peters182b5ac2004-07-18 06:16:08 +000083 command=lambda w=w: w.destroy())
Benjamin Petersond6d63f52009-01-04 18:53:28 +000084 box.pack(side=tkinter.tix.BOTTOM, fill=tkinter.tix.X)
85 top.pack(side=tkinter.tix.TOP, fill=tkinter.tix.BOTH, expand=1)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000086
87def select_month(event=None):
Martin v. Löwis8ec03e02002-03-17 18:19:13 +000088 # tixDemo:Status "Month = %s" % demo_month.get()
89 pass
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000090
91def select_year(event=None):
Martin v. Löwis8ec03e02002-03-17 18:19:13 +000092 # tixDemo:Status "Year = %s" % demo_year.get()
93 pass
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000094
95def ok_command(w):
Martin v. Löwis8ec03e02002-03-17 18:19:13 +000096 # tixDemo:Status "Month = %s, Year= %s" % (demo_month.get(), demo_year.get())
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000097 w.destroy()
98
99if __name__ == '__main__':
Benjamin Petersond6d63f52009-01-04 18:53:28 +0000100 root = tkinter.tix.Tk()
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000101 RunSample(root)
102 root.mainloop()