[wxPython] wxGrid ProcessTableMessage delete row ADDS ROWS

I'm using the ProcessTableMessage methods of wxPyGridTableBase to
notify my wxGrid that the data has changed.

wxGRIDTABLE_NOTIFY_ROWS_APPENDED works fine.

wxGRIDTABLE_REQUEST_VIEW_GET_VALUES also works fine.

wxGRIDTABLE_NOTIFY_ROWS_DELETED doesn't delete any rows.

Instead, it adds rows. And looking at the c++ code, it really does
expect a positive number as an argument. (and negative numbers don't
make it behave better).

Windows 98, Python 2.0, wxPython 2.2.5

See the appended modification of GridCustTable.py -- double clicks
SHOULD delete rows. See MyDeleteRows and OnLeftDClick.

# GridCustTable.py
from wxPython.wx import *
from wxPython.grid import *
import string

···

#---------------------------------------------------------------------------

class CustomDataTable(wxPyGridTableBase):
    """
    """

    def __init__(self, log):
        self.log = log
        self.log.write(" def __init__(self, log):\n")
        wxPyGridTableBase.__init__(self)
        self.log = log

        self.colLabels = ['ID', 'Description', 'Severity', 'Priority', 'Platform',
                          'Opened?', 'Fixed?', 'Tested?']

        self.dataTypes = [wxGRID_VALUE_NUMBER,
                          wxGRID_VALUE_STRING,
                          wxGRID_VALUE_CHOICE + ':only in a million years!,wish list,minor,normal,major,critical',
                          wxGRID_VALUE_NUMBER + ':1,5',
                          wxGRID_VALUE_CHOICE + ':all,MSW,GTK,other',
                          wxGRID_VALUE_BOOL,
                          wxGRID_VALUE_BOOL,
                          wxGRID_VALUE_BOOL]

        self.data = [
            [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1],
            [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0],
            [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0],
            [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1],
            [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0],
            [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0],
            [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1],
            [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0],
            [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0]

            ]

    #--------------------------------------------------
    # required methods for the wxPyGridTableBase interface

    def GetNumberRows(self):
        self.log.write(" def GetNumberRows(self):\n")
        return len(self.data)

    def GetNumberCols(self):
        self.log.write(" def GetNumberCols(self):\n")
        return len(self.data[0])

    def IsEmptyCell(self, row, col):
        self.log.write(" def IsEmptyCell(self, row, col):\n")
        return not self.data[row][col]

    # Get/Set values in the table. The Python version of these
    # methods can handle any data-type, (as long as the Editor and
    # Renderer understands the type too,) not just strings as in the
    # C++ version.
    def GetValue(self, row, col):
        self.log.write(" def GetValue(self, row, col):\n")
        try:
            return self.data[row][col]
        except IndexError:
            return ''

    def MyDeleteRows(self):
        del(self.data[-1])
        msg = wxGridTableMessage(self, # The table
                                 wxGRIDTABLE_NOTIFY_ROWS_DELETED, # what we did to it
                                 1) # how many
        self.GetView().ProcessTableMessage(msg)
        self.log.write("Deleted row\n")

    def SetValue(self, row, col, value):
        self.log.write(" def SetValue(self, row, col, value):\n")
        try:
            self.data[row][col] = value
        except IndexError:
            # add a new row
            self.data.append([''] * self.GetNumberCols())
            self.SetValue(row, col, value)
            
            # tell the grid we've added a row
            msg = wxGridTableMessage(self, # The table
                                     wxGRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it
                                     1) # how many
            
            self.GetView().ProcessTableMessage(msg)

    #--------------------------------------------------
    # Some optional methods

    # Called when the grid needs to display labels
    def GetColLabelValue(self, col):
        self.log.write(" def GetColLabelValue(self, col):\n")
        return self.colLabels[col]

    # Called to determine the kind of editor/renderer to use by
    # default, doesn't necessarily have to be the same type used
    # nativly by the editor/renderer if they know how to convert.
    def GetTypeName(self, row, col):
        self.log.write(" def GetTypeName(self, row, col):\n")
        return self.dataTypes[col]

    # Called to determine how the data can be fetched and stored by the
    # editor and renderer. This allows you to enforce some type-safety
    # in the grid.
    def CanGetValueAs(self, row, col, typeName):
        self.log.write(" def CanGetValueAs(self, row, col, typeName):\n")
        colType = string.split(self.dataTypes[col], ':')[0]
        if typeName == colType:
            return true
        else:
            return false

    def CanSetValueAs(self, row, col, typeName):
        self.log.write(" def CanSetValueAs(self, row, col, typeName):\n")
        return self.CanGetValueAs(row, col, typeName)

#---------------------------------------------------------------------------

class CustTableGrid(wxGrid):
    def __init__(self, parent, log):
        wxGrid.__init__(self, parent, -1)

        table = CustomDataTable(log)

        # The second parameter means that the grid is to take ownership of the
        # table and will destroy it when done. Otherwise you would need to keep
        # a reference to it and call it's Destroy method later.
        self.SetTable(table, true)
        self.table = table

        self.SetRowLabelSize(0)
        self.SetMargins(0,0)
        self.AutoSizeColumns(false)

        EVT_GRID_CELL_LEFT_DCLICK(self, self.OnLeftDClick)

    # I do this because I don't like the default behaviour of not starting the
    # cell editor on double clicks, but only a second click.
    def OnLeftDClick(self, evt):
# if self.CanEnableCellControl():
# self.EnableCellEditControl()
        self.table.MyDeleteRows()

#---------------------------------------------------------------------------

class TestFrame(wxFrame):
    def __init__(self, parent, log):
        wxFrame.__init__(self, parent, -1, "Custom Table, data driven Grid Demo", size=(640,480))
        grid = CustTableGrid(self, log)

#---------------------------------------------------------------------------

if __name__ == '__main__':
    import sys
    app = wxPySimpleApp()
    frame = TestFrame(None, sys.stdout)
    frame.Show(true)
    app.MainLoop()

#---------------------------------------------------------------------------

I had the same experience. In order to delete rows, the workaround is to use wxGRIDTABLE_NOTIFY_ROWS_APPENDED and supply a negative number of rows.

Wayne

Patricia Hawkins wrote:

···

I'm using the ProcessTableMessage methods of wxPyGridTableBase to
notify my wxGrid that the data has changed.

wxGRIDTABLE_NOTIFY_ROWS_APPENDED works fine.

wxGRIDTABLE_REQUEST_VIEW_GET_VALUES also works fine.

wxGRIDTABLE_NOTIFY_ROWS_DELETED doesn't delete any rows.

Instead, it adds rows. And looking at the c++ code, it really does
expect a positive number as an argument. (and negative numbers don't
make it behave better).

Windows 98, Python 2.0, wxPython 2.2.5

See the appended modification of GridCustTable.py -- double clicks
SHOULD delete rows. See MyDeleteRows and OnLeftDClick.

# GridCustTable.py
from wxPython.wx import *
from wxPython.grid import *
import string

#---------------------------------------------------------------------------

class CustomDataTable(wxPyGridTableBase):
    """

    def __init__(self, log):
        self.log = log
        self.log.write(" def __init__(self, log):\n")
        wxPyGridTableBase.__init__(self)
        self.log = log

        self.colLabels = ['ID', 'Description', 'Severity', 'Priority', 'Platform',
                          'Opened?', 'Fixed?', 'Tested?']

        self.dataTypes = [wxGRID_VALUE_NUMBER,
                          wxGRID_VALUE_STRING,
                          wxGRID_VALUE_CHOICE + ':only in a million years!,wish list,minor,normal,major,critical',
                          wxGRID_VALUE_NUMBER + ':1,5',
                          wxGRID_VALUE_CHOICE + ':all,MSW,GTK,other',
                          wxGRID_VALUE_BOOL,
                          wxGRID_VALUE_BOOL]

        self.data = [
            [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1],
            [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0],
            [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0],
            [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1],
            [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0],
            [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0],
            [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1],
            [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0],
            [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0]

            ]

    #--------------------------------------------------
    # required methods for the wxPyGridTableBase interface

    def GetNumberRows(self):
        self.log.write(" def GetNumberRows(self):\n")
        return len(self.data)

    def GetNumberCols(self):
        self.log.write(" def GetNumberCols(self):\n")
        return len(self.data[0])

    def IsEmptyCell(self, row, col):
        self.log.write(" def IsEmptyCell(self, row, col):\n")
        return not self.data[row][col]

    # Get/Set values in the table. The Python version of these
    # methods can handle any data-type, (as long as the Editor and
    # Renderer understands the type too,) not just strings as in the
    # C++ version.
    def GetValue(self, row, col):
        self.log.write(" def GetValue(self, row, col):\n")
        try:
            return self.data[row][col]
        except IndexError:
            return ''

    def MyDeleteRows(self):
        del(self.data[-1])
        msg = wxGridTableMessage(self, # The table
                                 wxGRIDTABLE_NOTIFY_ROWS_DELETED, # what we did to it
                                 1) # how many
        self.GetView().ProcessTableMessage(msg)
        self.log.write("Deleted row\n")

            def SetValue(self, row, col, value):
        self.log.write(" def SetValue(self, row, col, value):\n")
        try:
            self.data[row][col] = value
        except IndexError:
            # add a new row
            self.data.append([''] * self.GetNumberCols())
            self.SetValue(row, col, value)
                        # tell the grid we've added a row
            msg = wxGridTableMessage(self, # The table
                                     wxGRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it
                                     1) # how many
                        self.GetView().ProcessTableMessage(msg)

    #--------------------------------------------------
    # Some optional methods

    # Called when the grid needs to display labels
    def GetColLabelValue(self, col):
        self.log.write(" def GetColLabelValue(self, col):\n")
        return self.colLabels[col]

    # Called to determine the kind of editor/renderer to use by
    # default, doesn't necessarily have to be the same type used
    # nativly by the editor/renderer if they know how to convert.
    def GetTypeName(self, row, col):
        self.log.write(" def GetTypeName(self, row, col):\n")
        return self.dataTypes[col]

    # Called to determine how the data can be fetched and stored by the
    # editor and renderer. This allows you to enforce some type-safety
    # in the grid.
    def CanGetValueAs(self, row, col, typeName):
        self.log.write(" def CanGetValueAs(self, row, col, typeName):\n")
        colType = string.split(self.dataTypes[col], ':')[0]
        if typeName == colType:
            return true
        else:
            return false

    def CanSetValueAs(self, row, col, typeName):
        self.log.write(" def CanSetValueAs(self, row, col, typeName):\n")
        return self.CanGetValueAs(row, col, typeName)

#---------------------------------------------------------------------------

class CustTableGrid(wxGrid):
    def __init__(self, parent, log):
        wxGrid.__init__(self, parent, -1)

        table = CustomDataTable(log)

        # The second parameter means that the grid is to take ownership of the
        # table and will destroy it when done. Otherwise you would need to keep
        # a reference to it and call it's Destroy method later.
        self.SetTable(table, true)
        self.table = table

        self.SetRowLabelSize(0)
        self.SetMargins(0,0)
        self.AutoSizeColumns(false)

        EVT_GRID_CELL_LEFT_DCLICK(self, self.OnLeftDClick)

    # I do this because I don't like the default behaviour of not starting the
    # cell editor on double clicks, but only a second click.
    def OnLeftDClick(self, evt):
# if self.CanEnableCellControl():
# self.EnableCellEditControl()
        self.table.MyDeleteRows()

#---------------------------------------------------------------------------

class TestFrame(wxFrame):
    def __init__(self, parent, log):
        wxFrame.__init__(self, parent, -1, "Custom Table, data driven Grid Demo", size=(640,480))
        grid = CustTableGrid(self, log)

#---------------------------------------------------------------------------

if __name__ == '__main__':
    import sys
    app = wxPySimpleApp()
    frame = TestFrame(None, sys.stdout)
    frame.Show(true)
    app.MainLoop()

#---------------------------------------------------------------------------

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwindows.org
http://lists.wxwindows.org/mailman/listinfo/wxpython-users

I had the same experience. In order to delete rows, the workaround
is to use wxGRIDTABLE_NOTIFY_ROWS_APPENDED and supply a negative
number of rows.

Wayne

This should probably be entered in the bug tracker.

···

Patricia Hawkins wrote:

> I'm using the ProcessTableMessage methods of wxPyGridTableBase to
> notify my wxGrid that the data has changed.
>
> wxGRIDTABLE_NOTIFY_ROWS_APPENDED works fine.
>
> wxGRIDTABLE_REQUEST_VIEW_GET_VALUES also works fine.
>
> wxGRIDTABLE_NOTIFY_ROWS_DELETED doesn't delete any rows.
>
> Instead, it adds rows. And looking at the c++ code, it really does
> expect a positive number as an argument. (and negative numbers don't
> make it behave better).
>
> Windows 98, Python 2.0, wxPython 2.2.5
>
> See the appended modification of GridCustTable.py -- double clicks
> SHOULD delete rows. See MyDeleteRows and OnLeftDClick.
>

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!

Robin Dunn wrote:

I had the same experience. In order to delete rows, the workaround
is to use wxGRIDTABLE_NOTIFY_ROWS_APPENDED and supply a negative
number of rows.

Wayne

This should probably be entered in the bug tracker.

Done.