]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/ListBox.py
1 # 11/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
8 #---------------------------------------------------------------------------
10 # This listbox subclass lets you type the starting letters of what you want to
11 # select, and scrolls the list to the match if it is found.
12 class FindPrefixListBox(wx
.ListBox
):
13 def __init__(self
, parent
, id, pos
=wx
.DefaultPosition
, size
=wx
.DefaultSize
,
14 choices
=[], style
=0, validator
=wx
.DefaultValidator
):
15 wx
.ListBox
.__init
__(self
, parent
, id, pos
, size
, choices
, style
, validator
)
18 self
.Bind(wx
.EVT_KEY_DOWN
, self
.OnKey
)
21 def FindPrefix(self
, prefix
):
22 self
.log
.WriteText('Looking for prefix: %s\n' % prefix
)
25 prefix
= prefix
.lower()
28 # Changed in 2.5 because ListBox.Number() is no longer supported.
29 # ListBox.GetCount() is now the appropriate way to go.
30 for x
in range(self
.GetCount()):
31 text
= self
.GetString(x
)
34 if text
[:length
] == prefix
:
35 self
.log
.WriteText('Prefix %s is found.\n' % prefix
)
38 self
.log
.WriteText('Prefix %s is not found.\n' % prefix
)
43 key
= evt
.GetKeyCode()
45 if key
>= 32 and key
<= 127:
46 self
.typedText
= self
.typedText
+ chr(key
)
47 item
= self
.FindPrefix(self
.typedText
)
50 self
.SetSelection(item
)
52 elif key
== wx
.WXK_BACK
: # backspace removes one character and backs up
53 self
.typedText
= self
.typedText
[:-1]
55 if not self
.typedText
:
58 item
= self
.FindPrefix(self
.typedText
)
61 self
.SetSelection(item
)
66 def OnKeyDown(self
, evt
):
70 #---------------------------------------------------------------------------
72 class TestListBox(wx
.Panel
):
73 def __init__(self
, parent
, log
):
75 wx
.Panel
.__init
__(self
, parent
, -1)
77 sampleList
= ['zero', 'one', 'two', 'three', 'four', 'five',
78 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
79 'twelve', 'thirteen', 'fourteen']
81 wx
.StaticText(self
, -1, "This example uses the wxListBox control.", (45, 10))
82 wx
.StaticText(self
, -1, "Select one:", (15, 50), (65, 18))
83 self
.lb1
= wx
.ListBox(self
, 60, (80, 50), (80, 120), sampleList
, wx
.LB_SINGLE
)
84 self
.Bind(wx
.EVT_LISTBOX
, self
.EvtListBox
, self
.lb1
)
85 self
.Bind(wx
.EVT_LISTBOX_DCLICK
, self
.EvtListBoxDClick
, self
.lb1
)
86 self
.lb1
.Bind(wx
.EVT_RIGHT_UP
, self
.EvtRightButton
)
87 self
.lb1
.SetSelection(3)
88 self
.lb1
.Append("with data", "This one has data");
89 self
.lb1
.SetClientData(2, "This one has data");
92 wx
.StaticText(self
, -1, "Select many:", (200, 50), (65, 18))
93 self
.lb2
= wx
.ListBox(self
, 70, (280, 50), (80, 120), sampleList
, wx
.LB_EXTENDED
)
94 self
.Bind(wx
.EVT_LISTBOX
, self
.EvtMultiListBox
, self
.lb2
)
95 self
.lb2
.Bind(wx
.EVT_RIGHT_UP
, self
.EvtRightButton
)
96 self
.lb2
.SetSelection(0)
98 sampleList
= sampleList
+ ['test a', 'test aa', 'test aab',
99 'test ab', 'test abc', 'test abcc',
102 wx
.StaticText(self
, -1, "Find Prefix:", (15, 250))
103 fp
= FindPrefixListBox(self
, -1, (80, 250), (80, 120), sampleList
, wx
.LB_SINGLE
)
107 def EvtListBox(self
, event
):
108 self
.log
.WriteText('EvtListBox: %s, %s, %s\n' %
109 (event
.GetString(), event
.IsSelection(), event
.GetSelection()))
111 lb
= event
.GetEventObject()
112 data
= lb
.GetClientData(lb
.GetSelection())
115 self
.log
.WriteText('\tdata: %s\n' % data
)
118 def EvtListBoxDClick(self
, event
):
119 self
.log
.WriteText('EvtListBoxDClick: %s\n' % self
.lb1
.GetSelection())
120 self
.lb1
.Delete(self
.lb1
.GetSelection())
122 def EvtMultiListBox(self
, event
):
123 self
.log
.WriteText('EvtMultiListBox: %s\n' % str(self
.lb2
.GetSelections()))
125 def EvtRightButton(self
, event
):
126 self
.log
.WriteText('EvtRightButton: %s\n' % event
.GetPosition())
128 if event
.GetEventObject().GetId() == 70:
129 selections
= list(self
.lb2
.GetSelections())
132 for index
in selections
:
133 self
.lb2
.Delete(index
)
136 #---------------------------------------------------------------------------
138 def runTest(frame
, nb
, log
):
139 win
= TestListBox(nb
, log
)
142 #---------------------------------------------------------------------------
147 overview
= """<html><body>
148 A listbox is used to select one or more of a list of
149 strings. The strings are displayed in a scrolling box, with the
150 selected string(s) marked in reverse video. A listbox can be single
151 selection (if an item is selected, the previous selection is removed)
152 or multiple selection (clicking an item toggles the item on or off
153 independently of other selections).
158 if __name__
== '__main__':
161 run
.main(['', os
.path
.basename(sys
.argv
[0])])