]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxComboBox.py
Fix compile error when building for Python 2.2
[wxWidgets.git] / wxPython / demo / wxComboBox.py
1 # 11/15/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for wx namespace
4 #
5
6 import wx
7
8 #---------------------------------------------------------------------------
9
10 class TestComboBox(wx.Panel):
11 def OnSetFocus(self, evt):
12 print "OnSetFocus"
13 evt.Skip()
14
15 def OnKillFocus(self, evt):
16 print "OnKillFocus"
17 evt.Skip()
18
19 def __init__(self, parent, log):
20 self.log = log
21 wx.Panel.__init__(self, parent, -1)
22
23 sampleList = ['zero', 'one', 'two', 'three', 'four', 'five',
24 #'this is a long item that needs a scrollbar...',
25 'six', 'seven', 'eight']
26
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 )
35
36 ##import win32api, win32con
37 ##win32api.SendMessage(cb.GetHandle(),
38 ## win32con.CB_SETHORIZONTALEXTENT,
39 ## 200, 0)
40
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)
46
47 # Once the combobox is set up, we append some more data to it.
48 cb.Append("foo", "This is some client data for this item")
49
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.
55 for item in sampleList:
56 cb.Append(item, item.upper())
57
58 self.Bind(wx.EVT_COMBOBOX, self.EvtComboBox, cb)
59 self.Bind(wx.EVT_COMBOBOX, self.EvtText, cb)
60
61 # The user selects something, we go here.
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))
66
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.
71 def EvtText(self, evt):
72 self.log.WriteText('EvtText: %s\n' % evt.GetString())
73
74 # Capture events when the user types something into the control then
75 # hits ENTER.
76 def EvtTextEnter(self, evt):
77 self.log.WriteText('EvtTextEnter: %s' % evt.GetString())
78
79 #---------------------------------------------------------------------------
80
81 def runTest(frame, nb, log):
82 win = TestComboBox(nb, log)
83 return win
84
85 #---------------------------------------------------------------------------
86
87
88
89 overview = """\
90 A ComboBox is like a combination of an edit control and a listbox. It can be
91 displayed as static list with editable or read-only text field; or a drop-down
92 list with text field; or a drop-down list without a text field.
93
94 This 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
96 data). The former is common for read-only controls.
97
98 This example also shows the two form factors for the ComboBox. The first is more
99 common, and resembles a Choice control. The latter, although less common, shows
100 how all the values in the ComboBox can be visible, yet the functionality is the
101 same for both.
102
103 Finally, this demo shows how event handling can differ. The first ComboBox is set
104 up to handle EVT_TEXT_ENTER events, in which text is typed in and then ENTER is
105 hit by the user. This allows the user to enter a line of text which can then be
106 processed by the program. EVT_TEXT can also be processed, but in that case the
107 event is generated every time that the user hits a key in the ComboBox entry field.
108
109 """
110
111 if __name__ == '__main__':
112 import sys,os
113 import run
114 run.main(['', os.path.basename(sys.argv[0])])
115