wx.Grid, Hiding columns

I post a little example code to show what I mean (I simply modified one of the example of the demo), just run the code, press button 1 and then button 2 and, for example, try to use the scrollbar. That simulates exactly what happens in my program.

import wx
import wx.grid as gridlib

class HugeTable(gridlib.PyGridTableBase):

def __init__(self, log):
    gridlib.PyGridTableBase.__init__(self)
    self.log = log
    self._rows = self.GetNumberRows()
    self._cols = self.GetNumberCols()

def ResetView(self, grid):
    grid.BeginBatch()
    for current, new, delmsg, addmsg in [(self._rows, self.GetNumberRows(), gridlib.GRIDTABLE_NOTIFY_ROWS_DELETED, gridlib.GRIDTABLE_NOTIFY_ROWS_APPENDED),
        (self._cols, self.GetNumberCols(), gridlib.GRIDTABLE_NOTIFY_COLS_DELETED, gridlib.GRIDTABLE_NOTIFY_COLS_APPENDED),]:
        if new < current:
            msg = gridlib.GridTableMessage(self,delmsg,new,current-new)
            grid.ProcessTableMessage(msg)
        elif new > current:
            msg = gridlib.GridTableMessage(self,addmsg,new-current)
            grid.ProcessTableMessage(msg)
            self.UpdateValues(grid)
    grid.EndBatch()
    self._rows = self.GetNumberRows()
    self._cols = self.GetNumberCols()
    grid.AdjustScrollbars()
    grid.ForceRefresh()

def UpdateValues(self, grid):
    msg = gridlib.GridTableMessage(self, gridlib.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
    grid.ProcessTableMessage(msg)

def GetNumberRows(self):
    return 100

def GetNumberCols(self):
    return 20000

def GetValue(self, row, col):
    return str( (row, col) )

class HugeTableGrid(gridlib.Grid):
def init(self, parent, log):
gridlib.Grid.init(self, parent, -1)
self.table = HugeTable(log)
self.SetTable(self.table, True)

···

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

class TestFrame(wx.Frame):
def init(self, parent, log):
wx.Frame.init(self, parent, -1, “”, size=(840, 680))
sizer = wx.BoxSizer(wx.VERTICAL)
self.grid = HugeTableGrid(self, log)
self.grid.SetColMinimalAcceptableWidth(0)
b1 = wx.Button(self, -1, “1”)
b2 = wx.Button(self, -1, “2”)
sizer.Add(self.grid, 1, wx.ALL|wx.EXPAND, 5)
sizer.Add(b1, 0, wx.ALL, 5)
sizer.Add(b2, 0, wx.ALL, 5)
self.SetSizer(sizer)

self.Bind(wx.EVT_BUTTON, self.OnButton1, b1)
self.Bind(wx.EVT_BUTTON, self.OnButton2, b2)

def OnButton1(self, event) :
for i in range(0, 20000) :
self.grid.SetColSize(i, 0)
self.grid.table.ResetView(self.grid)

def OnButton2(self, event) :
self.grid.SetColSize(10, 100)
self.grid.table.ResetView(self.grid)

if name == ‘main’:
import sys
app = wx.PySimpleApp()
frame = TestFrame(None, sys.stdout)
frame.Show(True)
app.MainLoop()

I am afraid you want too much from your RAM: I tried your code, only reducing the number of columns to 200, and it runs smoothly.
Unless there’s a compelling necessity to have all data included at the same time in one grid (that nobody could swallow it in one gulp), I suggest that you put the data into a database, and trust the selection of the ones the user wants to see at any given moment to a SELECT query: the resulting output should arrive much faster.

BTW in databases the number of columns is usually fixed, because they describe the categories of data, while the rows -containing the actual data- vary: so rows tend to become more numerous than columns.

···

2008/10/20 Massi massi_srb@msn.com

I post a little example code to show what I mean (I simply modified one of the example of the demo), just run the code, press button 1 and then button 2 and, for example, try to use the scrollbar. That simulates exactly what happens in my program.

import wx
import wx.grid as gridlib

class HugeTable(gridlib.PyGridTableBase):

def __init__(self, log):
    gridlib.PyGridTableBase.__init__(self)
    self.log = log
    self._rows = self.GetNumberRows()
    self._cols = self.GetNumberCols()

def ResetView(self, grid):
    grid.BeginBatch()
    for current, new, delmsg, addmsg in [(self._rows, self.GetNumberRows(), gridlib.GRIDTABLE_NOTIFY_ROWS_DELETED, gridlib.GRIDTABLE_NOTIFY_ROWS_APPENDED),
        (self._cols, self.GetNumberCols(), gridlib.GRIDTABLE_NOTIFY_COLS_DELETED, gridlib.GRIDTABLE_NOTIFY_COLS_APPENDED),]:
        if new < current:
            msg = gridlib.GridTableMessage(self,delmsg,new,current-new)
            grid.ProcessTableMessage(msg)
        elif new > current:
            msg = gridlib.GridTableMessage(self,addmsg,new-current)
            grid.ProcessTableMessage(msg)
            self.UpdateValues(grid)
    grid.EndBatch()
    self._rows = self.GetNumberRows()
    self._cols = self.GetNumberCols()
    grid.AdjustScrollbars()
    grid.ForceRefresh()
def UpdateValues(self, grid):
    msg = gridlib.GridTableMessage(self, gridlib.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
    grid.ProcessTableMessage(msg)

def GetNumberRows(self):
    return 100
def GetNumberCols(self):
    return 20000
def GetValue(self, row, col):
    return str( (row, col) )

class HugeTableGrid(gridlib.Grid):
def init(self, parent, log):
gridlib.Grid.init(self, parent, -1)
self.table = HugeTable(log)
self.SetTable(self.table, True)

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

class TestFrame(wx.Frame):
def init(self, parent, log):
wx.Frame.init(self, parent, -1, “”, size=(840, 680))
sizer = wx.BoxSizer(wx.VERTICAL)
self.grid = HugeTableGrid(self, log)
self.grid.SetColMinimalAcceptableWidth(0)
b1 = wx.Button(self, -1, “1”)
b2 = wx.Button(self, -1, “2”)
sizer.Add(self.grid, 1, wx.ALL|wx.EXPAND, 5)
sizer.Add(b1, 0, wx.ALL, 5)
sizer.Add(b2, 0, wx.ALL, 5)
self.SetSizer(sizer)

self.Bind(wx.EVT_BUTTON, self.OnButton1, b1)
self.Bind(wx.EVT_BUTTON, self.OnButton2, b2)

def OnButton1(self, event) :
for i in range(0, 20000) :
self.grid.SetColSize(i, 0)
self.grid.table.ResetView(self.grid)

def OnButton2(self, event) :
self.grid.SetColSize(10, 100)
self.grid.table.ResetView(self.grid)

if name == ‘main’:
import sys
app = wx.PySimpleApp()
frame = TestFrame(None, sys.stdout)
frame.Show(True)
app.MainLoop()


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users