wxPython - ListBox & ListCtrl Class



A wx.ListBox widget presents a vertically scrollable list of strings. By default, a single item in the list is selectable. However, it can be customized to be multi-select.

ListCtrl widget is a highly enhanced list display and selection tool. List of more than one column can be displayed in Report view, List view or Icon view.

ListBox constructor has the following definition −

Wx.ListBox(parent, id, pos, size, choices, style)

Choices parameter is the list of strings used to populate the list.

wx.ListBox object is customizable with the following style parameters −

S.N. Parameters & Description
1

wxLB_SINGLE

Single-selection list

2

wxLB_MULTIPLE

Multiple-selection list: the user can toggle multiple items on and off

3

wxLB_EXTENDED

Extended-selection list − the user can extend the selection by using SHIFT or CTRL keys together with the cursor movement keys or the mouse

4

wxLB_HSCROLL

Create horizontal scrollbar if contents are too wide

5

wxLB_ALWAYS_SB

Always show a vertical scrollbar

6

wxLB_NEEDED_SB

Only creates a vertical scrollbar if needed

7

wxLB_SORT

The listbox contents are sorted in an alphabetical order

wx.ListBox class methods −

S.N. Methods & Description
1

DeSelect()

Deselects an item in the list box

2

InsertItem()

Inserts a given string at the specified position

3

SetFirstItem()

Sets a string at the given index as first in the list

4

IsSorted()

Returns true if wxzL?B_SORT style is used

5

GetString()

Returns the string at the selected index

6

SetString()

Sets the label for an item at the given index

EVT_LISTBOX binder triggers the handler when an item in the list is selected or when the selection changes programmatically. Handler function bound by EVT_LISTBOX_DCLICK is invoked when a double-click event on the list box item occurs.

Example

In the following example, a ListBox control and a TextCtrl object are respectively placed in the left and the right portion of a horizontal box sizer. ListBox is populated with strings in languages[] list object.

languages = ['C', 'C++', 'Java', 'Python', 'Perl', 'JavaScript','PHP','VB.NET','C#'] 
self.text = wx.TextCtrl(panel,style = wx.TE_MULTILINE) 
lst = wx.ListBox(panel, size = (100,-1), choices = languages, style = wx.LB_SINGLE)

Two objects are placed in a horizontal box sizer.

box = wx.BoxSizer(wx.HORIZONTAL) 
box.Add(lst,0,wx.EXPAND) 
box.Add(self.text, 1, wx.EXPAND)

ListBox control is linked to onListBox() handler with EVT_LISTBOX binder.

self.Bind(wx.EVT_LISTBOX, self.onListBox, lst)

The handler appends selected string into multiline TextCtrl on the right.

def onListBox(self, event): 
   self.text.AppendText( "Current selection: "+ 
      event.GetEventObject().GetStringSelection() + "\n")

The complete code is as follows −

import wx 
class Mywin(wx.Frame): 
            
   def __init__(self, parent, title): 
      super(Mywin, self).__init__(parent, title = title,size = (350,300))
		
      panel = wx.Panel(self) 
      box = wx.BoxSizer(wx.HORIZONTAL) 
		
      self.text = wx.TextCtrl(panel,style = wx.TE_MULTILINE) 
         
      languages = ['C', 'C++', 'Java', 'Python', 'Perl', 'JavaScript', 'PHP', 'VB.NET','C#']   
      lst = wx.ListBox(panel, size = (100,-1), choices = languages, style = wx.LB_SINGLE)
		
      box.Add(lst,0,wx.EXPAND) 
      box.Add(self.text, 1, wx.EXPAND) 
		
      panel.SetSizer(box) 
      panel.Fit() 
		
      self.Centre() 
      self.Bind(wx.EVT_LISTBOX, self.onListBox, lst) 
      self.Show(True)  
		
   def onListBox(self, event): 
      self.text.AppendText( "Current selection: 
         "+event.GetEventObject().GetStringSelection()+"\n")
		
ex = wx.App() 
Mywin(None,'ListBox Demo') 
ex.MainLoop()

The above code produces the following output −

Listbox Demo

wx.ListCtrl is an enhanced, and therefore, more complex widget. Where a ListBox shows only one column, ListCtrl can contain multiple columns. The appearance of ListCtrl widget is controlled by the following style parameters −

S.N. Parameters & Description
1

wx.LC_LIST

Multicolumn list view, with optional small icons. Columns are computed automatically

2

wx.LC_REPORT

Single or multicolumn report view, with optional header

3

wx.LC_VIRTUAL

The application provides items text on demand. May only be used with wxLC_REPORT

4

wx.LC_ICON

Large icon view, with optional labels

5

wx.LC_SMALL_ICON

Small icon view, with optional labels

6

wx.LC_ALIGN_LEFT

Icons align to the left

7

wx.LC_EDIT_LABELS

Labels are editable − the application will be notified when editing starts

8

wx.LC_NO_HEADER

No header in report mode

9

wx.LC_SORT_ASCENDING

Sort in ascending order

10

wx.LC_SORT_DESCENDING

Sort in descending order

11

wx.LC_HRULES

Draws light horizontal rules between the rows in report mode

12

wx.LC_VRULES

Draws light vertical rules between the columns in report mode

Example

A ListCtrl widget in report view is constructed in the following example.

self.list = wx.ListCtrl(panel, -1, style = wx.LC_REPORT)

Header columns are created by InsertColumn() method which takes the column number, caption, style and width parameters.

self.list.InsertColumn(0, 'name', width = 100) 
self.list.InsertColumn(1, 'runs', wx.LIST_FORMAT_RIGHT, 100) 
self.list.InsertColumn(2, 'wkts', wx.LIST_FORMAT_RIGHT, 100)

A list of tuples, each containg three strings, called players[] stores the data which is used to populate columns of the ListCtrl object.

New row starts with InsertStringItem() method which returns the index of the current row. Use of sys.maxint gives the row number after the last row. Using the index, other columns are filled by SetStringItem() method.

for i in players: 
   index = self.list.InsertStringItem(sys.maxint, i[0]) 
   self.list.SetStringItem(index, 1, i[1]) 
   self.list.SetStringItem(index, 2, i[2])

The complete code for the example is −

import sys 
import wx  

players = [('Tendulkar', '15000', '100'), ('Dravid', '14000', '1'), 
   ('Kumble', '1000', '700'), ('KapilDev', '5000', '400'), 
   ('Ganguly', '8000', '50')] 
	
class Mywin(wx.Frame): 
            
   def __init__(self, parent, title): 
      super(Mywin, self).__init__(parent, title = title) 
		
      panel = wx.Panel(self) 
      box = wx.BoxSizer(wx.HORIZONTAL)
		
      self.list = wx.ListCtrl(panel, -1, style = wx.LC_REPORT) 
      self.list.InsertColumn(0, 'name', width = 100) 
      self.list.InsertColumn(1, 'runs', wx.LIST_FORMAT_RIGHT, 100) 
      self.list.InsertColumn(2, 'wkts', wx.LIST_FORMAT_RIGHT, 100) 
         
      for i in players: 
         index = self.list.InsertStringItem(sys.maxint, i[0]) 
         self.list.SetStringItem(index, 1, i[1]) 
         self.list.SetStringItem(index, 2, i[2]) 
			
      box.Add(self.list,1,wx.EXPAND) 
      panel.SetSizer(box) 
      panel.Fit() 
      self.Centre() 
         
      self.Show(True)  
     
ex = wx.App() 
Mywin(None,'ListCtrl Demo') 
ex.MainLoop()

The above code produces the following output. Players’ data is displayed in report view −

Listctrl Demo Output
wxpython_major_classes.htm
Advertisements