How can I capture user keyboard navigation

In addition to using the mouse, the user can navigate through the grid with the keyboard. You can also programmatically change the cursor with the move methods listed in table 14.10. Many of the methods take an expandSelection parameter. This parameter works the same in each method. If the parameter is True, the current selection is stretched to include the new cursor position. If the parameter is False, the current selection is replaced by the new cursor.

Table 14.10 Grid cursor move methods

Method

Description

MoveCursorDown(expandSelection)

Moves cursor down. Equivalent to a down arrow keypress (without expanding selection) or a shift-down keypress (with expanding selection).

MoveCursorDownBlock(expandSelection)

Moves the cursor down to one cell further than the current vertical extent of the selection. Equivalent to a ctrl-down keypress (without expanding selection) or a shift-control-down keypress (with expanding selection).

MoveCursorLeft(expandSelection)

Moves cursor left. Equivalent to a left arrow keypress (without expanding selection) or a shift-left keypress (with expanding selection).

MoveCursorLeftBlock(expandSelection)

Moves the cursor left to one cell further than the current horizontal extent of the selection. Equivalent to a ctrl-left keypress (without expanding selection) or a shift-control-left keypress (with expanding selection).

MoveCursorRight(expandSelection)

Moves cursor right. Equivalent to a right arrow keypress (without expanding selection) or a shift-right keypress (with expanding selection).

MoveCursorRightBlock(expandSelection)

Moves the cursor right to one cell further than the current horizontal extent of the selection. Equivalent to a ctrl-right keypress (without expanding selection) or a shift-control-right keypress (with expanding selection).

Table 14.10 Grid cursor move methods (continued)

Method

Description

MoveCursorUp(expandSelection)

Moves cursor up. Equivalent to an up arrow keypress (without expanding selection) or a shift-up keypress (with expanding selection).

MoveCursorUpBlock(expandSelection)

Moves the cursor up to one cell further than the current vertical extent of the selection. Equivalent to a ctrl-up keypress (without expanding selection) or a shift-control-up keypress (with expanding selection).

MovePageDown()

Moves the cursor selection down, such that the cells at the bottom of the display move to the top of the display.

MovePageUp()

Moves the cursor selection up, such that the cells at the top of the display move to the bottom of the display.

That covers nearly all of what you need to know about grids. In the next chapter, we'll tackle the next widget, the tree control.

14.5 Summary

■ The grid control allows you to create spreadsheet-like grid tables with a great deal of control and flexibility. The grid control is an instance of the class wx.grid.Grid. Typically grid controls are complex enough that it's worthwhile to create your own custom grid subclass with its own_init_

method rather than just creating an instance of the base class and calling its methods elsewhere in your application.

■ There are two ways to populate a grid control with data. The control can be built explicitly with the CreateGrid(numRows, numCols) method, after which individual cells can be set with the SetCellValue(row, col, s) method. Alternately, you can create an instance of a grid table, which acts as a model for the grid, and allows you to easily use data from another source in the grid display. A grid table is a subclass of wx.grid.PyGridTableBase with methods such as GetValue(row, col) which can be overridden to drive the grid behavior when displaying a cell. The table is connected to the grid control using the method SetTable(table). When a grid is created with a table, then the table gets veto power over changes to the grid's size with the row and column creation and removal methods.

■ A grid has row and column labels that have default values similar to what you would expect in a spreadsheet. The display text and other display attributes of the labels can be changed with grid properties. The size of a row or column can be set explicitly for each item, or the grid can auto size based on the displayed data. The user can also be allowed to change the size of the grid by dragging the grid lines. You can set a minimum size for each row or column if desired, to prevent cells from becoming too small to display their data. In addition, specific cells can be set to overflow into other rows or columns using the SetCellSize(row, col, numrows, numcols) method.

■ The user can select one or more rectangles of cells in the grid, and that process can be duplicated programmatically with a number of different Select methods. A grid cell that is scrolled off the screen can be moved onto the display with the MakeCellVisible(row, col) method.

■ Much of the power and flexibility of the grid control comes from the ability to create custom renderers and editors for each cell. A renderer controls the display of information in the cell. The default renderer is just a simple string, but there are predefined renderers for Boolean, integer, and floating point data. You can create your own renderer by subclassing wx.Grid. PyGridCellRenderer and overwriting one method for drawing.

■ By default, a wxPython grid allows in-place editing of the data. You can change that property on a cell-by-cell basis, or by row or column, or for the entire grid. The editor object governs what control the user sees when editing in place. The default is a normal text edit control for modifying strings. There are predefined editors for Boolean, integer, and floating point data, as well as one which gives users a finite choice of options. You can create your own custom renderer by subclassing wx.grid.GridCellEditor and overriding several of the base class methods.

■ The grid control has a number of different events that you can capture, including separate events for mouse clicks in the cells and in the labels, and events triggered by changing the size of a cell. In addition, you can trigger navigation of the cursor in the grid programmatically.

Was this article helpful?

0 0

Post a comment