]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/ComboBox.py
Don't decref if the interpreter is not initialized, (eg. it's in its
[wxWidgets.git] / wxPython / demo / ComboBox.py
CommitLineData
8fa876ca
RD
1
2import wx
cf694132
RD
3
4#---------------------------------------------------------------------------
5
8fa876ca 6class TestComboBox(wx.Panel):
059a841c
RD
7 def OnSetFocus(self, evt):
8 print "OnSetFocus"
9 evt.Skip()
8fa876ca 10
059a841c
RD
11 def OnKillFocus(self, evt):
12 print "OnKillFocus"
13 evt.Skip()
14
cf694132
RD
15 def __init__(self, parent, log):
16 self.log = log
8fa876ca 17 wx.Panel.__init__(self, parent, -1)
cf694132
RD
18
19 sampleList = ['zero', 'one', 'two', 'three', 'four', 'five',
8fa876ca 20 #'this is a long item that needs a scrollbar...',
cf694132
RD
21 'six', 'seven', 'eight']
22
95bfd958 23 wx.StaticText(self, -1, "This example uses the wx.ComboBox control.", (8, 10))
8fa876ca
RD
24 wx.StaticText(self, -1, "Select one:", (15, 50), (75, 18))
25
26 # This combobox is created with a preset list of values.
27 cb = wx.ComboBox(
28 self, 500, "default value", (90, 50),
29 (95, -1), sampleList, wx.CB_DROPDOWN #|wxTE_PROCESS_ENTER
30 )
cf694132 31
8fa876ca
RD
32 self.Bind(wx.EVT_COMBOBOX, self.EvtComboBox, cb)
33 self.Bind(wx.EVT_TEXT, self.EvtText, cb)
34 self.Bind(wx.EVT_TEXT_ENTER, self.EvtTextEnter, cb)
35 cb.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
36 cb.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
059a841c 37
0d6ff082 38 # Once the combobox is set up, we can append some more data to it.
3bbeffe4 39 cb.Append("foo", "This is some client data for this item")
059a841c 40
8fa876ca
RD
41 # This combobox is created with no values initially.
42 cb = wx.ComboBox(
a5d9c77b 43 self, 501, "default value", (90, 80), (95, -1), [], wx.CB_DROPDOWN)
8fa876ca
RD
44
45 # Here we dynamically add our values to the second combobox.
68bc8549 46 for item in sampleList:
1e4a197e 47 cb.Append(item, item.upper())
cf694132 48
8fa876ca 49 self.Bind(wx.EVT_COMBOBOX, self.EvtComboBox, cb)
cf694132 50
0d6ff082 51 # When the user selects something, we go here.
3bbeffe4
RD
52 def EvtComboBox(self, evt):
53 cb = evt.GetEventObject()
28ff70c2 54 data = cb.GetClientData(evt.GetSelection())
3bbeffe4 55 self.log.WriteText('EvtComboBox: %s\nClientData: %s\n' % (evt.GetString(), data))
cf694132 56
8fa876ca
RD
57 if evt.GetString() == 'one':
58 self.log.WriteText("You follow directions well!\n\n")
59
60 # Capture events every time a user hits a key in the text entry field.
3bbeffe4
RD
61 def EvtText(self, evt):
62 self.log.WriteText('EvtText: %s\n' % evt.GetString())
985eb16c 63 evt.Skip()
f6bcfd97 64
8fa876ca
RD
65 # Capture events when the user types something into the control then
66 # hits ENTER.
468c1af0 67 def EvtTextEnter(self, evt):
8fa876ca 68 self.log.WriteText('EvtTextEnter: %s' % evt.GetString())
985eb16c 69 evt.Skip()
468c1af0 70
cf694132
RD
71#---------------------------------------------------------------------------
72
73def runTest(frame, nb, log):
74 win = TestComboBox(nb, log)
75 return win
76
77#---------------------------------------------------------------------------
78
79
8fa876ca
RD
80overview = """\
81A ComboBox is like a combination of an edit control and a listbox. It can be
82displayed as static list with editable or read-only text field; or a drop-down
83list with text field; or a drop-down list without a text field.
cf694132 84
8fa876ca
RD
85This example shows both a preset ComboBox and one that is dynamically created
86(that is, it is initially empty but then we 'grow' it out of program-supplied
87data). The former is common for read-only controls.
cf694132 88
8fa876ca
RD
89This example also shows the two form factors for the ComboBox. The first is more
90common, and resembles a Choice control. The latter, although less common, shows
91how all the values in the ComboBox can be visible, yet the functionality is the
92same for both.
cf694132 93
8fa876ca
RD
94Finally, this demo shows how event handling can differ. The first ComboBox is set
95up to handle EVT_TEXT_ENTER events, in which text is typed in and then ENTER is
96hit by the user. This allows the user to enter a line of text which can then be
97processed by the program. EVT_TEXT can also be processed, but in that case the
98event is generated every time that the user hits a key in the ComboBox entry field.
cf694132 99
1e6796a0 100"""
cf694132 101
95bfd958
RD
102#---------------------------------------------------------------------------
103
1e6796a0
RD
104if __name__ == '__main__':
105 import sys,os
106 import run
8eca4fef 107 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
cf694132 108