Search code examples
pythonwxpython

Why does SetCellEditor cause error when reapplied after self.m_grid_3.ClearGrid()


The following code populates a grid each time the combobox is used to select a new value. The first time the code runs it works fine and creates a populated grid with a drop down in each cell in col 4. However when I select a second new value and the function executes the self.m_grid_3.ClearGrid() and repopulates I get the following error.

  self.m_grid3.SetCellEditor(row, col, self.choice_editor)
  File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\grid.py", line 2000, in SetCellEditor
  return _grid.Grid_SetCellEditor(*args, **kwargs)
  TypeError: in method 'Grid_SetCellEditor', expected argument 4 of type 'wxGridCellEditor *'

Selecting a dropdown in col4 then crashes python.

Any ideas how I can fix this.

Here is the code in question.

class Inspection(BulkUpdate):
    def __init__(self, parent):
        BulkUpdate.__init__(self, parent)
        list = EmployeeList()
        list_climbers = list.get_climbers()
        for name in list_climbers:
            self.edit_kit_comboBox.Append(str(name.employee))
        choices = ["Yes", "No", "Not Checked"]
        self.choice_editor = wx.grid.GridCellChoiceEditor(choices, True)


    def on_engineer_select( self, event ):
        self.m_grid3.ClearGrid()
        person = self.edit_kit_comboBox.GetValue()
        list = KitList()
        equipment = list.list_of_equipment(person, 1)
        rows = len(equipment)

        for row in range(0, rows):
            for col in range(0, 5):
                print "row = %s col = %s" % (row, col)
                if col == 4:
                    self.m_grid3.SetCellValue(row, col+2, str(equipment[row][col]))
                    self.m_grid3.SetCellValue(row, col, "Pass")
                    self.m_grid3.SetCellEditor(row, col, self.choice_editor)
                else:
                    self.m_grid3.SetCellValue(row, col, str(equipment[row][col]))

The code stops on the second loop while populating the grid the second time. I have been trying to work this out for days.


Solution

  • Try adding this to __init__:

    self.choice_editor.IncRef()
    

    My guess is that the C++ portion of the editor object is being deleted when you call ClearGrid. Giving it that extra reference tells the Grid that you want to hold on to it.