■ The list control is wxPython's widget for displaying lists of information. It is more complex and fully featured than the simpler list box widget. List controls are instances of the class wx.ListCtrl. A list control can be displayed in icon mode, with each item text displaying below an icon, or small icon mode with smaller icons. In list mode the elements display in columns, wrapping from the bottom of one to the top of the next and in report mode, the list displays in multi-column format with column headers and one row per item.
■ The images for a list control are managed in an image list, which is an array of images accessible via an integer index. The list control can maintain separate image lists for different list modes, allowing for easy switching between, say, large and small icon mode.
■ You insert text into the list using the method InsertStringItem(index, label), and an image using the InsertImageItem(index, imagelndex) method. To add both at once, you can use InsertImageStringItem(index, label, imagelndex). To add columns in report mode, you use the method InsertColumn(col, heading, format="wx.LIST_FORMAT_LEFT, width=-1). Once the column has been added you can add text to new columns using the method SetStringItem(index, col, label, imageId=-1).
■ List controls generate several events that can be bound to program actions. The event items are of class wx.ListEvent. Common event types include evt_list_insert_item, evt_list_item_activated, and evt_list_
■ If the list control is declared with the flag wx.lc_edit_labels, then the user will be able to edit the text of list items when they are selected. The edit is accepted by pressing enter or clicking elsewhere in the control, it is canceled by pressing the escape key.
■ You can sort lists by declaring them with the flag wx.LC_SORT_ASCENDlNG or wx.lc_sort_descending. This will sort items based on their string order. In report mode, the strings in column 0 will be used. You can use the method Sortltems(func) to create your own custom sorting method. For a list in report mode, the mixin class wx.lib.mixins.listctrl.ColumnSorterMixin can give you the ability to sort on a user-selected column.
■ A list control declared with the flag wx.lc_virtual is a virtual list control. This means that its data is determined dynamically at runtime when items in the list are displayed. For a virtual list control, you must override the method OnGetltemText(item, col) to return the proper text to display in the given row and column. You can also override the methods OnGetltem-Image(item) or OnGetltemAttr(item) to return the image or list display attributes for each row. If the underlying data changes, you can update a single row of the list using the method Refreshltem(item) and multiple rows with the method RefreshItems(itemFrom, itemTo).
Eventually, your data will become too complex to be stored in a mere list. You'll need something designed for a full two-dimensional spreadsheet style display, called the grid control, which we'll discuss in the next chapter.
the grid control
This chapter covers
■ Adding rows and cells and managing column headers
■ Using a custom cell renderer
■ Creating custom editors
■ Capturing user events
The grid widget is perhaps the most complex and flexible single widget in wxPython. In this chapter, you'll get a chance to see and work with many of the grid widget's features. We'll talk about how you enter data into the grid control and how you manage the display attributes of the control, and we'll talk about custom editors and renderers. At base, the grid control allows you to display tabular data in a spreadsheet-like grid format. The widget allows you to specify labels for rows and columns, change the grid size by dragging grid lines, and specify font and color attributes for each cell individually.
In most cases, you will display your value as a simple string. However, you can also specify a custom renderer for any cell that allows you to display data differently; for example, displaying a Boolean value as a checkbox. You can edit a grid cell in-place in the table, and you can use different types of editors for different kinds of data. You can also create your own custom renderers and editors to give you nearly unlimited flexibility in the display and management of cell data. Grids also have a large amount of mouse and keyboard events that you can capture and use to trigger code in your application.
We'll start our discussion by showing two ways to create a wxPython grid.
Was this article helpful?