]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxListBox.py 
   2  from  wxPython
. wx 
import  *    6  #---------------------------------------------------------------------------    8  class  wxFindPrefixListBox ( wxListBox
):    9      def  __init__ ( self
,  parent
,  id ,  pos
= wxDefaultPosition
,  size
= wxDefaultSize
,   10                   choices
=[],  style
= 0 ,  validator
= wxDefaultValidator
):   11          wxListBox
.__ init
__ ( self
,  parent
,  id ,  pos
,  size
,  choices
,  style
,  validator
)   13          EVT_KEY_UP ( self
,  self
. OnKey
)   16      def  FindPrefix ( self
,  prefix
):   18              prefix 
=  string
. lower ( prefix
)   20              for  x 
in  range ( self
. Number ()):   21                  text 
=  self
. GetString ( x
)   22                  text 
=  string
. lower ( text
)   23                  if  text
[: length
] ==  prefix
:   29          key 
=  evt
. GetKeyCode ()   30          if  key 
>=  32  and  key 
<=  127 :   31              self
. typedText 
=  self
. typedText 
+  chr ( key
)   32              item 
=  self
. FindPrefix ( self
. typedText
)   34                  self
. SetSelection ( item
)   36          elif  key 
==  WXK_BACK
:    # backspace removes one character and backs up   37              self
. typedText 
=  self
. typedText
[:- 1 ]   38              if not  self
. typedText
:   41                  item 
=  self
. FindPrefix ( self
. typedText
)   43                      self
. SetSelection ( item
)   49  #---------------------------------------------------------------------------   51  class  TestListBox ( wxPanel
):   52      def  __init__ ( self
,  parent
,  log
):   54          wxPanel
.__ init
__ ( self
,  parent
, - 1 )   56          sampleList 
= [ 'zero' ,  'one' ,  'two' ,  'three' ,  'four' ,  'five' ,   57                        'six' ,  'seven' ,  'eight' ,  'nine' ,  'ten' ,  'eleven' ,   58                        'twelve' ,  'thirteen' ,  'fourteen' ]   60          wxStaticText ( self
, - 1 ,  "This example uses the wxListBox control." ,   63          wxStaticText ( self
, - 1 ,  "Select one:" ,  wxPoint ( 15 ,  50 ),  wxSize ( 65 ,  18 ))   64          self
. lb1 
=  wxListBox ( self
,  60 ,  wxPoint ( 80 ,  50 ),  wxSize ( 80 ,  120 ),   65                         sampleList
,  wxLB_SINGLE
)   66          EVT_LISTBOX ( self
,  60 ,  self
. EvtListBox
)   67          EVT_LISTBOX_DCLICK ( self
,  60 ,  self
. EvtListBoxDClick
)   68          EVT_RIGHT_UP ( self
. lb1
,  self
. EvtRightButton
)   69          self
. lb1
. SetSelection ( 3 )   70          self
. lb1
. Append ( "with data" ,  "This one has data" );   71          self
. lb1
. SetClientData ( 2 ,  "This one has data" );   74          wxStaticText ( self
, - 1 ,  "Select many:" ,  wxPoint ( 200 ,  50 ),  wxSize ( 65 ,  18 ))   75          self
. lb2 
=  wxListBox ( self
,  70 ,  wxPoint ( 280 ,  50 ),  wxSize ( 80 ,  120 ),   76                         sampleList
,  wxLB_EXTENDED
)   77          EVT_LISTBOX ( self
,  70 ,  self
. EvtMultiListBox
)   78          EVT_RIGHT_UP ( self
. lb2
,  self
. EvtRightButton
)   79          self
. lb2
. SetSelection ( 0 )   82          sampleList 
=  sampleList 
+ [ 'test a' ,  'test aa' ,  'test aab' ,   83                                     'test ab' ,  'test abc' ,  'test abcc' ,   86          wxStaticText ( self
, - 1 ,  "Find Prefix:" ,  wxPoint ( 15 ,  250 ))   87          fp 
=  wxFindPrefixListBox ( self
, - 1 ,  wxPoint ( 80 ,  250 ),  wxSize ( 80 ,  120 ),   88                                   sampleList
,  wxLB_SINGLE
)   92      def  EvtListBox ( self
,  event
):   93          self
. log
. WriteText ( 'EvtListBox:  %s \n '  %  event
. GetString ())   94          lb 
=  event
. GetEventObject ()   95          data 
=  lb
. GetClientData ( lb
. GetSelection ())   97              self
. log
. WriteText ( ' \t data:  %s \n '  %  data
)  100      def  EvtListBoxDClick ( self
,  event
):  101          self
. log
. WriteText ( 'EvtListBoxDClick:  %s \n '  %  self
. lb1
. GetSelection ())  102          self
. lb1
. Delete ( self
. lb1
. GetSelection ())  104      def  EvtMultiListBox ( self
,  event
):  105          self
. log
. WriteText ( 'EvtMultiListBox:  %s \n '  %  str ( self
. lb2
. GetSelections ()))  107      def  EvtRightButton ( self
,  event
):  108          self
. log
. WriteText ( 'EvtRightButton:  %s \n '  %  event
. GetPosition ())  109          if  event
. GetEventObject (). GetId () ==  70 :  110              selections 
=  list ( self
. lb2
. GetSelections ())  112              for  index 
in  selections
:  113                  self
. lb2
. Delete ( index
)  116  #---------------------------------------------------------------------------  118  def  runTest ( frame
,  nb
,  log
):  119      win 
=  TestListBox ( nb
,  log
)  122  #---------------------------------------------------------------------------  138  A listbox is used to select one or more of a list of strings. The strings are displayed in a scrolling box, with the selected string(s) marked in reverse video. A listbox can be single selection (if an item is selected, the previous selection is removed) or multiple selection (clicking an item toggles the item on or off independently of other selections).