wxpython grid GRIDTABLE_NOTIFY_ROWS_DELETED

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Piet

    #1

    wxpython grid GRIDTABLE_NOTIFY_ROWS_DELETED

    Hello,
    I have written a small program which dynamically displays data from a
    data structure in a wx.grid. The grid reacts as expected when the data
    structure is enlarged: a new row is added and filled with data. When
    data is removed, the data is deleted as expected, but instead of being
    removed, a row is being added to the grid. Can anyone explain why?
    Here is sample code to illustrate what I mean.
    Thanks in advance
    Piet
    import wx
    import wx.grid
    #---------------------------------------------------------------------------

    class XmlTableModel(w x.grid.PyGridTa bleBase):
    def __init__(self,h eaders,data):
    wx.grid.PyGridT ableBase.__init __(self)
    self.headers = headers
    self.data = data

    def GetNumberRows(s elf):
    return len(self.data)

    def GetNumberCols(s elf):
    return len(self.header s)

    def RemoveData(self ,rowNum):
    self.data.pop()
    self.GetView(). ProcessTableMes sage(wx.grid.Gr idTableMessage( self,wx.grid.GR IDTABLE_NOTIFY_ ROWS_DELETED,1) )

    def AddData(self,da ta):
    self.data.appen d(data)
    self.GetView(). ProcessTableMes sage(wx.grid.Gr idTableMessage( self,wx.grid.GR IDTABLE_NOTIFY_ ROWS_APPENDED,1 ))

    def IsEmptyCell(sel f, row, col):
    try:
    if self.data[row][col] != "":
    return True
    else:
    return False
    except:
    return False

    def GetValue(self, row, col):
    return self.data[row][col]

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

    def GetColLabelValu e(self, col):
    return self.headers[col]

    def GetDefaultColLa belSize(self):
    return 100

    def GetRowLabelValu e(self, row):
    return self.data[row][0]
    #---------------------------------------------------------------------------

    class TestFrame(wx.Fr ame):
    def __init__(self, parent):
    wx.Frame.__init __(self, parent, -1, "Simple Table Model Demo",
    size=(640,480))
    self.panel = wx.Panel(self, -1, style=0)
    self.grid = wx.grid.Grid(se lf.panel)
    self.headers = ["Eins","Zwei"," Drei","Vier"]
    self.data = [["A1","A2","A3", "A4"],
    ["B1","B2","B3", "B4"],
    ["C1","C2","C3", "C4"],
    ["D1","D2","D3", "D4"]]
    self.grid.SetTa ble(XmlTableMod el(self.headers ,self.data),
    True)
    self.AddBtn = wx.Button(self. panel, -1, "Add Entry")
    self.AddBtn.Set Default()
    self.Bind(wx.EV T_BUTTON, self.OnAddEntry , self.AddBtn)
    self.RemoveBtn = wx.Button(self. panel, -1, "Remove Entry")
    self.Bind(wx.EV T_BUTTON, self.OnRemoveEn try, self.RemoveBtn)
    self.bs = wx.BoxSizer(wx. VERTICAL)
    self.bs.Add(sel f.grid, 1, wx.GROW|wx.ALL, 5)
    self.bs.Add(sel f.AddBtn)
    self.bs.Add(sel f.RemoveBtn)
    self.panel.SetS izer(self.bs)

    def OnAddEntry(self , evt):
    self.grid.GetTa ble().AddData(["NA1","NA2","NA 3","NA4"])

    def OnRemoveEntry(s elf, evt):
    self.grid.GetTa ble().RemoveDat a(0)

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

    if __name__ == '__main__':
    import sys
    app = wx.PySimpleApp( )
    frame = TestFrame(None)
    frame.Show(True )
    app.MainLoop()
    #---------------------------------------------------------------------------
  • Jean Brouwers

    #2
    Re: wxpython grid GRIDTABLE_NOTIF Y_ROWS_DELETED


    Change the message call in RemoveData() to

    self.GetView(). ProcessTableMes sage(wx.grid.Gr idTableMessage(
    self,wx.grid.GR IDTABLE_NOTIFY_ ROWS_DELETED, len(data), 1))

    The ..._ROW_DELETED message requires two args, the index of the first
    row and the number of rows to be removed. Similarly for colums.

    /Jean Brouwers
    ProphICy Semiconductor, Inc.

    PS) Also, check the DoUpdate() method in this example

    <http://www.bitpim.org/pyxr/c/projects/bitpim/bpmedia.py.html >


    In article <39cbe663.04112 10343.6a1de0ed@ posting.google. com>, Piet
    <pit.grinja@gmx .de> wrote:
    [color=blue]
    > Hello,
    > I have written a small program which dynamically displays data from a
    > data structure in a wx.grid. The grid reacts as expected when the data
    > structure is enlarged: a new row is added and filled with data. When
    > data is removed, the data is deleted as expected, but instead of being
    > removed, a row is being added to the grid. Can anyone explain why?
    > Here is sample code to illustrate what I mean.
    > Thanks in advance
    > Piet
    > import wx
    > import wx.grid
    > #---------------------------------------------------------------------------
    >
    > class XmlTableModel(w x.grid.PyGridTa bleBase):
    > def __init__(self,h eaders,data):
    > wx.grid.PyGridT ableBase.__init __(self)
    > self.headers = headers
    > self.data = data
    >
    > def GetNumberRows(s elf):
    > return len(self.data)
    >
    > def GetNumberCols(s elf):
    > return len(self.header s)
    >
    > def RemoveData(self ,rowNum):
    > self.data.pop()
    >
    > self.GetView(). ProcessTableMes sage(wx.grid.Gr idTableMessage( self,wx.grid.GR IDT
    > ABLE_NOTIFY_ROW S_DELETED,1))
    >
    > def AddData(self,da ta):
    > self.data.appen d(data)
    >
    > self.GetView(). ProcessTableMes sage(wx.grid.Gr idTableMessage( self,wx.grid.GR IDT
    > ABLE_NOTIFY_ROW S_APPENDED,1))
    >
    > def IsEmptyCell(sel f, row, col):
    > try:
    > if self.data[row][col] != "":
    > return True
    > else:
    > return False
    > except:
    > return False
    >
    > def GetValue(self, row, col):
    > return self.data[row][col]
    >
    > def SetValue(self, row, col, value):
    > self.data[row][col] = value
    >
    > def GetColLabelValu e(self, col):
    > return self.headers[col]
    >
    > def GetDefaultColLa belSize(self):
    > return 100
    >
    > def GetRowLabelValu e(self, row):
    > return self.data[row][0]
    > #---------------------------------------------------------------------------
    >
    > class TestFrame(wx.Fr ame):
    > def __init__(self, parent):
    > wx.Frame.__init __(self, parent, -1, "Simple Table Model Demo",
    > size=(640,480))
    > self.panel = wx.Panel(self, -1, style=0)
    > self.grid = wx.grid.Grid(se lf.panel)
    > self.headers = ["Eins","Zwei"," Drei","Vier"]
    > self.data = [["A1","A2","A3", "A4"],
    > ["B1","B2","B3", "B4"],
    > ["C1","C2","C3", "C4"],
    > ["D1","D2","D3", "D4"]]
    > self.grid.SetTa ble(XmlTableMod el(self.headers ,self.data),
    > True)
    > self.AddBtn = wx.Button(self. panel, -1, "Add Entry")
    > self.AddBtn.Set Default()
    > self.Bind(wx.EV T_BUTTON, self.OnAddEntry , self.AddBtn)
    > self.RemoveBtn = wx.Button(self. panel, -1, "Remove Entry")
    > self.Bind(wx.EV T_BUTTON, self.OnRemoveEn try, self.RemoveBtn)
    > self.bs = wx.BoxSizer(wx. VERTICAL)
    > self.bs.Add(sel f.grid, 1, wx.GROW|wx.ALL, 5)
    > self.bs.Add(sel f.AddBtn)
    > self.bs.Add(sel f.RemoveBtn)
    > self.panel.SetS izer(self.bs)
    >
    > def OnAddEntry(self , evt):
    > self.grid.GetTa ble().AddData(["NA1","NA2","NA 3","NA4"])
    >
    > def OnRemoveEntry(s elf, evt):
    > self.grid.GetTa ble().RemoveDat a(0)
    >
    > #---------------------------------------------------------------------------
    >
    > if __name__ == '__main__':
    > import sys
    > app = wx.PySimpleApp( )
    > frame = TestFrame(None)
    > frame.Show(True )
    > app.MainLoop()
    > #---------------------------------------------------------------------------[/color]

    Comment

    Working...