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