How do I manage the row and column headers of a grid

In a wxPython grid control, each row and column has its own label. By default, rows are given numeric labels starting with 1 and columns are given alphabetical labels starting with A and continuing to Z, which is followed by aa, ab, and so on. If you're creating a spreadsheet, this is great, but not necessary for most other applications. For something a bit less generic, wxPython provides methods to change the labels. Figure 14.3 displays a sample grid with label headers.

□ Grid Headers [

homer

marge

bart

lisa

maggie

uno

(uno .homer)

(uno,marge)

(uno,bart)

(uno .lisa)

(uno,maggie)

dos

(dos .homer)

(dos,marge)

(dos,bart)

(dos,lisa)

(dos .maggie)

tres

(tres .homer)

(tres .marge)

(tres.bart)

(tres,lisa)

(tres .maggie)

quatro

(quatro.homer)

(quatro.marge)

(quatro,bart)

(quatro,lisa)

(quatro .maggie)

cinco

(cinco .homer)

(cinco,marge)

(cinco,bart)

(cinco,lisa)

(cinco,maggie)

Figure 14.3 A sample grid with custom labels created

Listing 14.3 contains the code used to build the figure—in this example, the grid was initialized with CreateGrid().

Listing 14.3 Code for a sample non-model grid with custom labels import wx import wx.grid class TestFrame(wx.Frame):

rowLabels = ["uno", "dos", "tres", "quatro", "cinco"] colLabels = ["homer", "marge", "bart", "lisa", "maggie"]

wx.Frame._init_(self, None, title="Grid Headers", size=(500,2 00)) grid = wx.grid.Grid(self) grid.CreateGrid(5,5) for row in range(5):

grid.SetRowLabelValue(row, self.rowLabels[row]) grid.SetColLabelValue(row, self.colLabels[row]) for col in range(5):

grid.SetCellValue(row, col,

"(%s,%s)" % (self.rowLabels[row], self.colLabels[col]))

app = wx.PySimpleApp() frame = TestFrame() frame.Show() app.MainLoop()

As with adding and deleting rows, changing labels is done differently based on the type of grid. For grids that are created with CreateGrid(), set the label values using the methods SetColLabelValue(col, value) and SetRowLabelValue(row, value). O The col and row parameters are the index of the appropriate column or row, and the value parameter is the string to be displayed in that label. To get the labels for a row or a column, use the methods GetColLabelValue(col) and GetRowLabelValue(row).

For a grid control using an external grid table, you can achieve the same effect by overriding the grid table methods GetColLabelValue(col) and GetRowLabel-Value(row). To clear up any confusion, these methods are called internally by the grid control when it needs to display a label and the grid has an associated table. Since the returned value is determined dynamically by the code you put in the overridden method, there's no need to override or call the setter methods. The setter methods still exist—still SetColLabelValue(col, value) and SetRowLabel-Value(row, value)—but you would rarely use them, and only if you want the user to be able to change the underlying data. Typically, you don't need the setters. Listing 14.4 displays how to change labels in a table grid—this listing produces the same output as the previous listing.

Listing 14.4 Code for grid with a grid table that has custom labels import wx import wx.grid class TestTable(wx.grid.PyGridTableBase):

wx.grid.PyGridTableBase.__init__(self)

self.rowLabels = ["uno", "dos", "tres", "quatro", "cinco"] self.colLabels = ["homer", "marge", "bart", "lisa", "maggie"]

def GetNumberRows(self): return 5

def GetNumberCols(self): return 5

def IsEmptyCell(self, row, col): return False def GetValue(self, row, col):

return "(%s,%s)" % (self.rowLabels[row], self.colLabels[col])

def SetValue(self, row, col, value): pass def GetColLabelValue(self, col): <1— Column labels return self.colLabels[col]

def GetRowLabelValue(self, row): <1— Row labels return self.rowLabels[row]

class TestFrame(wx.Frame):

wx.Frame._init_(self, None, title="Grid Table"

size=(500,2 00)) grid = wx.grid.Grid(self) table = TestTable() grid.SetTable(table, True)

app = wx.PySimpleApp() frame = TestFrame() frame.Show() app.MainLoop()

By default, the labels are centered in their spaces; however, you can change that behavior by using the methods SetColumnLabelAlignment(horiz, vert) and Set-RowLabelAlignment(horiz, vert). In both cases, the horiz parameter controls the horizontal alignment and can have the values wx.align_left, wx.align_centre, or wx.ALIGN_RIGHT. The vert parameter controls the vertical alignment and can have the values wx.align_top, wx.align_centre, or wx.align_bottom.

The row and column label areas share a common set of attributes for color and font display. You can manage those properties with the setter methods Set-LabelBackgroundColour(colour), SetLabelFont(font), and SetLabelTextColour (colour) , each of which modifies the property implied by its name. The colour attribute is an instance of wx.Colour or something that wxPython converts to a color, such as the string name of a color. The font property is an instance of wx.Font. The associated getter methods, GetLabelBackgoundColour(), GetLabel-Font(), and GetLabelTextFont(), all exist.

+1 0

Post a comment