]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxListBox.py
2 from wxPython
.wx
import *
4 #---------------------------------------------------------------------------
6 class wxFindPrefixListBox(wxListBox
):
7 def __init__(self
, parent
, id, pos
=wxDefaultPosition
, size
=wxDefaultSize
,
8 choices
=[], style
=0, validator
=wxDefaultValidator
):
9 wxListBox
.__init
__(self
, parent
, id, pos
, size
, choices
, style
, validator
)
12 EVT_KEY_DOWN(self
, self
.OnKey
)
15 def FindPrefix(self
, prefix
):
16 self
.log
.WriteText('Looking for prefix: %s\n' % prefix
)
18 prefix
= prefix
.lower()
20 for x
in range(self
.Number()):
21 text
= self
.GetString(x
)
23 if text
[:length
] == prefix
:
24 self
.log
.WriteText('Prefix %s is found.\n' % prefix
)
26 self
.log
.WriteText('Prefix %s is not found.\n' % prefix
)
31 key
= evt
.GetKeyCode()
32 if key
>= 32 and key
<= 127:
33 self
.typedText
= self
.typedText
+ chr(key
)
34 item
= self
.FindPrefix(self
.typedText
)
36 self
.SetSelection(item
)
38 elif key
== WXK_BACK
: # backspace removes one character and backs up
39 self
.typedText
= self
.typedText
[:-1]
40 if not self
.typedText
:
43 item
= self
.FindPrefix(self
.typedText
)
45 self
.SetSelection(item
)
51 def OnKeyDown(self
, evt
):
55 #---------------------------------------------------------------------------
57 class TestListBox(wxPanel
):
58 def __init__(self
, parent
, log
):
60 wxPanel
.__init
__(self
, parent
, -1)
62 sampleList
= ['zero', 'one', 'two', 'three', 'four', 'five',
63 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
64 'twelve', 'thirteen', 'fourteen']
66 wxStaticText(self
, -1, "This example uses the wxListBox control.",
69 wxStaticText(self
, -1, "Select one:", wxPoint(15, 50), wxSize(65, 18))
70 self
.lb1
= wxListBox(self
, 60, wxPoint(80, 50), wxSize(80, 120),
71 sampleList
, wxLB_SINGLE
)
72 EVT_LISTBOX(self
, 60, self
.EvtListBox
)
73 EVT_LISTBOX_DCLICK(self
, 60, self
.EvtListBoxDClick
)
74 EVT_RIGHT_UP(self
.lb1
, self
.EvtRightButton
)
75 self
.lb1
.SetSelection(3)
76 self
.lb1
.Append("with data", "This one has data");
77 self
.lb1
.SetClientData(2, "This one has data");
80 wxStaticText(self
, -1, "Select many:", wxPoint(200, 50), wxSize(65, 18))
81 self
.lb2
= wxListBox(self
, 70, wxPoint(280, 50), wxSize(80, 120),
82 sampleList
, wxLB_EXTENDED
)
83 EVT_LISTBOX(self
, 70, self
.EvtMultiListBox
)
84 EVT_RIGHT_UP(self
.lb2
, self
.EvtRightButton
)
85 self
.lb2
.SetSelection(0)
88 sampleList
= sampleList
+ ['test a', 'test aa', 'test aab',
89 'test ab', 'test abc', 'test abcc',
92 wxStaticText(self
, -1, "Find Prefix:", wxPoint(15, 250))
93 fp
= wxFindPrefixListBox(self
, -1, wxPoint(80, 250), wxSize(80, 120),
94 sampleList
, wxLB_SINGLE
)
98 def EvtListBox(self
, event
):
99 self
.log
.WriteText('EvtListBox: %s, %s, %s\n' %
100 (event
.GetString(), event
.IsSelection(), event
.GetSelection()))
102 lb
= event
.GetEventObject()
103 data
= lb
.GetClientData(lb
.GetSelection())
105 self
.log
.WriteText('\tdata: %s\n' % data
)
108 def EvtListBoxDClick(self
, event
):
109 self
.log
.WriteText('EvtListBoxDClick: %s\n' % self
.lb1
.GetSelection())
110 self
.lb1
.Delete(self
.lb1
.GetSelection())
112 def EvtMultiListBox(self
, event
):
113 self
.log
.WriteText('EvtMultiListBox: %s\n' % str(self
.lb2
.GetSelections()))
115 def EvtRightButton(self
, event
):
116 self
.log
.WriteText('EvtRightButton: %s\n' % event
.GetPosition())
117 if event
.GetEventObject().GetId() == 70:
118 selections
= list(self
.lb2
.GetSelections())
120 for index
in selections
:
121 self
.lb2
.Delete(index
)
124 #---------------------------------------------------------------------------
126 def runTest(frame
, nb
, log
):
127 win
= TestListBox(nb
, log
)
130 #---------------------------------------------------------------------------
138 overview
= """<html><body>
139 A listbox is used to select one or more of a list of
140 strings. The strings are displayed in a scrolling box, with the
141 selected string(s) marked in reverse video. A listbox can be single
142 selection (if an item is selected, the previous selection is removed)
143 or multiple selection (clicking an item toggles the item on or off
144 independently of other selections).
149 if __name__
== '__main__':
152 run
.main(['', os
.path
.basename(sys
.argv
[0])])