How do I create a grid with a grid table

In more complicated cases, you can keep your data in a grid table, which is a separate class that stores the data and interacts with the grid control to display the data. The grid table is especially recommended where:

■ the data from the grid is complex

■ the data is already stored in other objects in your system

■ the grid is large enough that it should not all be stored in memory at once

In chapter 5, we discussed grid tables in the context of the MVC design pattern, along with different ways of implementing a grid table in your application. In this chapter, we'll focus on the nuts and bolts of using a grid table. Figure 14.2 displays a grid created using a grid table.

To use a grid table, create your own subclass of wx.grid.PyGridTableBase. Your subclass must override a few of the methods of the parent wx.grid.GridTableBase class. Listing 14.2 displays the code used to create figure 14.2.

Listing 14.2 The code for using the grid table mechanism import wx import wx.grid class TestTable(wx.grid.PyGridTableBase): <1— Defining the table grid def _init_(self):

wx.grid.PyGridTableBase._init_(self)

self.odd=wx.grid.GridCellAttr() self.odd.SetBackgroundColour("sky blue")

self.odd.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))

self.even=wx.grid.GridCellAttr() self.even.SetBackgroundColour("sea green")

self.even.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))

def GetNumberRows(self): return 5 0

def GetNumberCols(self): return 50

def IsEmptyCell(self, row, col):

return self.data.get((row, col)) is not None def GetValue(self, row, col): <-l Providing data for the grid value = self.data.get((row, col)) if value is not None:

return value else:

def SetValue(self, row, col, value): self.data[(row,col)] = value def GetAttr(self, row, col, kind):

attr.IncRef()

return attr class TestFrame(wx.Frame):

wx.Frame._init_(self, None, title="Grid Table", size=(640,480))

grid = wx.grid.Grid(self)

table = TestTable()

grid.SetTable(table, True) a— Assigning the table app = wx.PySimpleApp() frame = TestFrame() frame.Show() app.MainLoop()

In listing 14.2, all the application-specific logic has been moved to the grid table class, so there's no need to also create a custom subclass of wx.grid.Grid.

Figure 14.2 A simple grid using the grid table mechanism

For the grid table to be valid, there are five methods that you must override. Table 14.1 lists those methods. As we'll see in this chapter, there are other grid table methods that you can override to give your table more functionality.

To attach the grid table instance to your table instance call the method Set-

Table(table, takeOwnership=False, selmode=wx.grid.Grid.SelectCells) instead of calling CreateGrid(). The table parameter is your instance of wx.grid.PyGrid-TableBase. The takeOwnership parameter causes the grid control to own the table.

Table 14.1 Methods to override in wx.grid.GridTableBase

Method

Description

GetNumberCols()

Returns the number of columns to display in the grid

GetNumberRows()

Returns the number of rows to display in the grid

GetValue(row, col)

Returns the value to display at the row and col coordinate passed as parameters

IsEmptyCell(row, col)

Returns True if the cell at the row and col coordinate is supposed to be empty. In many cases, this method will be hardwired to always return False.

SetValue(row, col, value)

Allows you to update your underlying data structure to match the user edit, if you want to. For a read-only table, you must still declare this method, but you can have it pass and do nothing. Automatically called when the user edits a cell.

Wxpython Table Grid

If the parameter is set to True, the table is deleted by the wxPython system when the grid is deleted. The selmode parameter works the same way in SetTable() as it does in CreateGrid().

There are a few other methods you may want to override to manage various parts of the grid other than the table data itself. Later in this chapter, we'll discuss some of those methods along with that different functionality. And, we'll see that in some cases a table created with SetTable behaves differently than one created with CreateGrid().

One additional method that you can override is Clear(), which is called when the ClearGrid () is called on the grid—you would override this method to clean up the underlying data source, if appropriate. Now that you've gotten your data into the grid, you can start to do all sorts of fun stuff with it. In the next section, we'll show you how to manipulate the look of your grid.

Was this article helpful?

+7 -6

Responses

  • nora
    How to build table using wxpython?
    3 years ago

Post a comment