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