The grid control is used to display a two-dimensional dataset. To use this control to display useful information, you need to tell the control what data it is based on. In wxPython, two different mechanisms are used to process data in Grid controls. They are slightly different in the ways of adding, deleting, and Editing data.
The grid control can directly process values in each row and column.
Data can be processed indirectly by using a grid table.
We will discuss the second method. As for how to implement the details, I will not go into details here (there are already many examples on the internet). I will only explain some main steps:
Create a subclass of wx. grid. PyGridTableBase and override some methods of the parent class.
Bind the subclass and Grid implemented above to the SetTable method.
If the mesh is initialized using the CreateGrid () method, the InsertRows, DeleteRows, and other methods of the mesh can always work, in addition, the cells created in the new row or column start with an empty string. If the network is initialized using the SetTable () method, the grid table must support table changes. To support changes, your grid table (subclass of PyGridTableBase) must overwrite the same change method. For example, if you call the InsertCols () method for your mesh, you must declare an InsertCols (pos = 0, numCols = 1) method. This method of the grid table returns a Boolean value of True, indicating that the change is supported. If the return value is False, the change is rejected.
The following uses the InsertRows method as an example. "self. data" is a list, and each element is a dictionary. The implementation steps are as follows:
Add an element (newData) with an empty value and add it to the data variable (self. data)
Create a message to be inserted (gridtable_policy_rows_inserted) and assign values to it to notify the grid of new data insertion (similar to postmessage or sendmessage in mfc)
Create a query message (GRIDTABLE_REQUEST_VIEW_GET_VALUES) to notify the grid query and display new data.
Def InsertRows (self, pos = 1, numRows = 1 ):
""""""
For num in range (0, numRows ):
NewData = {};
NewData [u'lable'] = u''
Self. data. insert (pos, newData)
GridView = self. GetView ()
GridView. BeginBatch ()
InsertMsg = wx. grid. GridTableMessage (self, wx. grid. gridtable_policy_rows_inserted, pos, numRows)
GridView. ProcessTableMessage (insertMsg)
GridView. EndBatch ()
GetValueMsg = wx. grid. GridTableMessage (self, wx. grid. GRIDTABLE_REQUEST_VIEW_GET_VALUES)
GridView. ProcessTableMessage (getValueMsg)
Return True
Def AppendRows (self, numRows = 1 ):
""""""
For num in range (0, numRows ):
NewData = {};
NewData [u'lable'] = u''
Self. data. append (newData)
Self. isModified = True
GridView = self. GetView ()
GridView. BeginBatch ()
AppendMsg = wx. grid. GridTableMessage (self, wx. grid. gridtable_policy_rows_appended, numRows)
GridView. ProcessTableMessage (appendMsg)
GridView. EndBatch ()
GetValueMsg = wx. grid. GridTableMessage (self, wx. grid. GRIDTABLE_REQUEST_VIEW_GET_VALUES)
GridView. ProcessTableMessage (getValueMsg)
If self. onGridValueChanged:
Self. onGridValueChanged ()
Return True
Def DeleteRows (self, pos = 0, numRows = 1 ):
If self. data is None or len (self. data) = 0:
Return False
For rowNum in range (0, numRows ):
Self. data. remove (self. data [pos + rowNum])
GridView = self. GetView ()
GridView. BeginBatch ()
DeleteMsg = wx. grid. GridTableMessage (self, wx. grid. gridtable_policy_rows_deleted, pos, numRows)
GridView. ProcessTableMessage (deleteMsg)
GridView. EndBatch ()
GetValueMsg = wx. grid. GridTableMessage (self, wx. grid. GRIDTABLE_REQUEST_VIEW_GET_VALUES)
GridView. ProcessTableMessage (getValueMsg)
If self. onGridValueChanged:
Self. onGridValueChanged ()
Return True