wx.ListCtrl in wxPython

In wxPython the wxListCtrl can be a handy widget to display such things as data or music.

Example List Ctrl from the wxPython demo

But how do you use a list control… here are some basics…


To add a column to the List Control you can do this

# First create the list control in REPORT style

list_control = wx.ListCtrl(parent, id, …, style = wx.LC_REPORT)

list_control.InsertColumn(0, “Column 1”)

list_control.InsertColumn(1, “Column 2”)

list_control.InsertColumn(2, “Column 3”)

To add text data you can call

list_control.Append( “Column 1”, “Column 2”, “Column 3”)

and that would add a new row of data to the first three columns. Simple right? To do more advance stuff we first have to look at how a list control is organized…

Editing ListCtrl data (List Control Items)

The wx ListCtrl is made up of items each cell (intersection of a row and a column) is an item. To get an item you can get

item = list_control.GetItem(ROW, COL)

Then to manipulate the item you run one of the methods the ListCtrl has

For example

# First get the item

item = list_control.GetItem(ROW, COL)

# Then say change the text

list_control.SetItemText(item, “This is the new text”)

For a full list of all the available functions you can go to…


Keeping track of rows

To keep track of rows and columns (say if you use the List Control Column Sorter Mixin) you can set the Data

# Set Item Data

list_control.SetItemData(item, data)


# Later retrieve the data

data = list_control.GetItemData(item)

This lets you identify what the row and column are even if the rows or columns are reorganized.

And that is pretty much all you need to know…