PyGridTableBase can't automatically resize when grid is update by Grid.AutoSize().

I think Grid.AutoSize() resize grid when grid is update. But it doesn’t work.
This code is that test menu on Menubar click to update grid. Updated grid’s column size is not match SetColSize.
Maybe my code is wrong. Please tell me it.

#!/usr/bin/env python2.7

coding: UTF-8

import wx
import wx.grid as wxgrid

class GridTable(wxgrid.PyGridTableBase):

def __init__(self, update=None): 
    wxgrid.PyGridTableBase.__init__(self) 

    self.cols_labels = ['0', '1', '2', '3'] 
    self.data = [['0', '1', '2', '3'], ['4', '5', '6', '7'], ['8', '9', '10', '11'], ['12', '13', '14', '15']] 

    self.update = update 

def GetNumberRows(self): 
    return 4 

def GetNumberCols(self): 
    return 4 

def GetValue(self, row, col): 
    if self.update is None: 
        return self.data[row][col] 
    else: 
        return self.update[row][col] 

def GetColLabelValue(self, col): 
    return self.cols_labels[col] 

def GetRowLabelValue(self, row): 
    return row 

class Viewer(wx.Panel):

def __init__(self, *args, **kwags): 
    wx.Panel.__init__(self, *args, **kwags) 

    self.grid = wxgrid.Grid(self) 
    self.table = GridTable() 
    self.grid.SetTable(self.table) 

    self.grid.SetColSize(0, 50) 
    self.grid.SetColSize(1, 50) 
    self.grid.SetColSize(2, 50) 
    self.grid.SetColSize(3, 50) 
    self.grid.Refresh() 

    sizer = wx.BoxSizer(wx.VERTICAL) 
    sizer.Add(self.grid, 1, wx.EXPAND) 
    self.SetSizerAndFit(sizer) 

def update(self): 
    self.data = [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j', 'k', 'l'], ['m', 'n', 'o', 'p']] 
    self.grid.ClearGrid() 
    self.table = GridTable(self.data) 
    self.grid.SetTable(self.table) 
    self.grid.AutoSize() 
    # self.grid.AutoSizeColumns() 

class MainWindow(wx.Frame):

def __init__(self, *args, **kwargs): 
    wx.Frame.__init__(self, *args, **kwargs) 

    self.CreateStatusBar() # a Statusbar in the bottom of the window 

    # setting up the menu. 
    file_menu = wx.Menu() 
    file_menu.Append(wx.ID_OPEN, '&Test', 'Test') 

    # creating the menubar. 
    menu_bar = wx.MenuBar() 
    menu_bar.Append(file_menu, "&File") 
    self.SetMenuBar(menu_bar)  # adding the MenuBar to the Frame content. 
    self.Connect(wx.ID_OPEN, -1, wx.wxEVT_COMMAND_MENU_SELECTED, self.open) 

    sizer = wx.BoxSizer(wx.VERTICAL) 
    self.viewer = Viewer(self) 
    sizer.Add(self.viewer, 1, wx.EXPAND) 

def open(self, event): 
    self.viewer.update() 

if name == ‘main’:
app = wx.App(False)
frame = MainWindow(None, title=‘test’, size=(300, 300))
frame.Show()
app.MainLoop()

Did you read the documentation?
Like, where it says
You are creating a new GridTable object, and then attempting to
replace the first one. A better plan is to keep the same GridTable,
but just update its data. If I replace your “update” function with
this, it works fine:
def update(self): self.data = [[‘a’, ‘b’, ‘c’, ‘d’], [‘e’, ‘f’, ‘g’, ‘h’],
[‘i’, ‘j’, ‘k’, ‘l’], [‘m’, ‘n’, ‘o’, ‘p’]] self.table.data = self.data
self.grid.Refresh() You would probably want to add a method to GridTable to do this.

···

hudif wrote:

      I think

Grid.AutoSize() resize grid when grid is update. But it
doesn’t work.

      This code is that test menu on Menubar click to update grid.

Updated grid’s column size is not match SetColSize.

      Maybe my code is wrong. Please tell me it.

http://wiki.wxpython.org/wxGrid

Note: you can only call SetTable once for any
given wxGrid . Because of this, it is generally
necessary to have your wxPyGridTableBase class alter the grid’s
size and shape to reflect any changes in your table’s
dimensions. See wxGrid Size/Shape Management below for details.

-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com