How do I change the color or font of a grid cell

As with other controls, there are a set of properties that you can use to change the display attributes for each cell. Figure 14.5 displays some of what you can do with the attribute methods.

Listing 14.6 displays the code used to create figure 14.5. Notice the use of both grid methods aimed at a specific cell and of the creation of wx.grid.Grid-CellAttr objects.

Figure 14.5 A sample usage of the grid attribute methods

Listing 14.6 Changing the color of grid cells import wx import wx.grid class TestFrame(wx.Frame):

wx.Frame._init_(self, None, title="Grid Attributes", size=(600,300)) grid = wx.grid.Grid(self) grid.CreateGrid(10,6) for row in range(10):

for col in range(6):

grid.SetCellValue(row, col, "(%s,%s)" % (row, col))

grid.SetCellTextColour(1, 1, "red")

grid.SetCellFont(1,1, wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD)) grid.SetCellBackgroundColour(2, 2, "light blue")

attr = wx.grid.GridCellAttr()



attr.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))

grid.SetAttr(4, 0, attr) grid.SetAttr(5, 1, attr) grid.SetRowAttr(8, attr)

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

Wxpython Color Cell

Figure 14.5 A sample usage of the grid attribute methods

We'll begin by discussing the methods used for setting the default values for the entire grid. You can set the default alignment for all the cells in the grid with

SetDefaultCellAlignment(horiz, vert) where horiz is wx.LEFT, wx.CENTRE, or wx.RIGHT, and vert is wx.TOP, wx.CENTRE, or wx.BOTTOM. You can recover the default cell alignment with GetDefaultCellAlignment() which returns a tuple of (horiz, vert).

The background and text color can be set with the methods SetDefaultCell-TextColour(colour) and SetDefaultCellBackgroundColour(colour). As usual, the color can either be a wx.Colour instance or the string name of a color. The getters are GetDefaultCellTextColour() and GetDefaultCellBackgroundColour(). Finally, you can manage the default font with SetDefaultCellFont(font) and GetDefault-CellFont().

Using the following methods, you can set all of these attributes for an individual cell.

GetCellAlignment(row, col)

SetCellAlignment(row, col, horiz, vert)

GetCellBackgroundColour(row, col)

SetCellBackgroundColour(row, col, colour)

GetCellFont(row, col)

SetCellFont(row, col, font)

GetCellTextColour(row, col)

SetCellTextColour(row, col, colour)

Each method behaves as the default, with the addition of the row and col attributes that define the coordinates of the cell being discussed.

Selected cells have different background and foreground colors, which can be changed. The methods are SetSelectionBackground(colour) and SetSelection-Foreground(colour) , with the associated getter methods GetSelectionBack-ground() and GetSelectionForeground().

You can place extra space around the grid control by using the method Set-Margins(extraWidth, extraHeight)—the parameters indicate the amount of pixels that are used to pad the grid within its container.

Internally, the wx.grid.Grid class uses a class called wx.grid.GridCellAttr to manage the attributes of each cell. That class has getter and setter methods for all of the properties discussed in this section. You can get the cell attr object for a specific cell by using the method GetOrCreateCellAttr(row, col), which provides you with the cell attribute object for the appropriate cell, creating an object if necessary. A cell attribute object is only created if the cell has defined properties different from the grid default. Once you have the cell attribute object, you can use it to define display properties of the cell.

To create your own cell attribute object, the constructor is wx.grid.GridCell-Attr(). You can set some parameters, then pass the object to the methods Set-ColAttr(attr) or SetRowAttr(attr), which applies the display attributes to every cell in that row or column, as displayed in listing 14.6.

If you are using a grid table, you can override the method GetAttr(row, col) to return a wx.grid.GridCellAttr instance specific to that particular cell, which is used by the grid in displaying that cell.

You can also change the color and display of the grid lines. The display of the grid lines is controlled with the method EnableGridLines(enable). The enable parameter is a Boolean. If True, the lines are displayed, if False they are not displayed. You can change the color of the grid lines with SetGridLine-Color(colour) .

+2 0


  • nuguse
    What are wxgrid colours?
    7 years ago
  • natalia
    How to change the grid color in python?
    7 months ago
  • GARY
    How to change colour of cell in wx Grid control?
    2 months ago

Post a comment