Why is wxPython asking me to call CreateGrid() when the grid I'm working on already exists ?

Hi,

I have this function, which receives an empty wx.Grid (in panel), and
is supposed to fill some cells on this grid:

    def display_program_banner(self, panel):
        print(". type(panel):",type(panel))
        print(". panel.__dict__:",panel.__dict__)
        number_rows = panel.GetNumberRows()
        number_cols = panel.GetNumberCols()
        print(". number_rows:",number_rows)
        print(". number_cols:",number_cols)
        panel.AppendRows(4)
        panel.AppendCols(1)
        panel.SetCellValue(0,0,"svm_ts_tool")
        panel.SetCellValue(1,0,"Version 1.0")
        panel.SetCellValue(2,0,"DPM1 save_state date")
        panel.SetCellValue(3,0,"DPM2 save_state date")
        panel.AutoSize()
        panel.SetColLabelSize(0)
        panel.SetRowLabelSize(0)
        panel.Show(True)

However, I get the following error on the bolded line:

('. type(panel):', <class 'wx.grid.Grid'>)
('. panel.__dict__:', {'this': <Swig Object of type 'wxGrid *' at
0x355b1d8>})
('. number_rows:', 0)
('. number_cols:', 0)
Traceback (most recent call last):
  File "svm_ts_tool_in_progress.py", line 319, in OnSelChanged
    self.TreeViewController(self.tablecont)
  File "svm_ts_tool_in_progress.py", line 442, in TreeViewController
    self.TableViewController(req_table)
  File "svm_ts_tool_in_progress.py", line 1455, in TableViewController
    self.display_program_banner(self.table)
  File "svm_ts_tool_in_progress.py", line 1466, in
display_program_banner
    panel.AppendRows(4)
  File "c:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\grid.py",
line 1221, in AppendRows
    return _grid.Grid_AppendRows(*args, **kwargs)
wx._core.PyAssertionError: C++ assertion "wxAssertFailure" failed at ..
\..\src\generic\grid.cpp(6713) in wxGrid::AppendRows(): Called
wxGrid::AppendRows() before calling CreateGrid()

Why is wxPython asking me to call CreateGrid() when the grid I'm
working on (panel) already exists ?

Thanks,
Ron.

Why is wxPython asking me to call CreateGrid() when the grid I'm
working on (panel) already exists ?

wxGrid requires first to be constructed, as you said you did, and then
you call .CreateGrid() to tell it how many rows and columns it should
have (note the part in between asterisks):

wxGrid::CreateGrid

bool CreateGrid(int numRows, int numCols, wxGrid::wxGridSelectionModes
selmode = wxGrid::wxGridSelectCells)

Creates a grid with the specified initial number of rows and columns.
**Call this directly after the grid constructor**. When you use this function
wxGrid will create and manage a simple table of string values for you. All
of the grid data will be stored in memory.

Thanks for the answer.
I solved my problem in another way.
(BTW, if I try to call CreateGrid above, wxPython complains that I try
to call CreateGrid for a second time!).

Well, I don't understand how you used a grid without calling CreateGrid(),
nor why wxPython would complain--I thought one had to always call CreateGrid()
after construction of a grid. I'd be interested in understanding this.

Che

That's correct. More precisely, you need to give the grid a table before you can do anything with the grid's content or etc. You can do that by creating your own table class and using SetTable, or you can use the default internal string table class by calling CreateGrid.

···

On 11/9/09 8:08 AM, C M wrote:

Thanks for the answer.
I solved my problem in another way.
(BTW, if I try to call CreateGrid above, wxPython complains that I try
to call CreateGrid for a second time!).

Well, I don't understand how you used a grid without calling CreateGrid(),
nor why wxPython would complain--I thought one had to always call CreateGrid()
after construction of a grid.

--
Robin Dunn
Software Craftsman