Resize of a grid in a collapsible pane

Hello,

I have a collapsible pane which contains a grid (a pygridtablebase).
When the grid data are updated, the size of the grid does not change.
How to resize it according to the new data ?

I attach a sample for you to test it.
Thanks in advance
Dominique

#! /usr/bin/env python
# -*- coding: utf-8 -*-
import wx
import wx.aui
import wx.grid
import wx.lib.scrolledpanel as scrolled

···

#-----------------------------------------------
class LineupEntry(object):
    def __init__(self,position,one,two,three,four,five,six,seven):
        self.position=position
        self.one=one
        self.two=two
        self.three=three
        self.four=four
        self.five=five
        self.six=six
        self.seven=seven

mylist0=[LineupEntry("1",u"blabla",u"blabla",u"blabla",u"blabla",
u"blabla",u"blabla",u"blibli"),
        
LineupEntry("2",u"blibli",u"blibli",u"blibli",u"blibli",
u"blibli",u"blibli",u"blibli"),
        
LineupEntry("3",u"blabla",u"blabla",u"blabla",u"blabla",
u"blabla",u"blabla",u"blabla")]
mylist1=[LineupEntry("1",u"blabla",u"blabla",u"blabla",
u"blibli",u"blibli",u"blibli",u"blibli"),
        
LineupEntry("2",u"blibli",u"blibli",u"blibli",u"blibli",
u"blibli",u"blibli",u"blibli"),
        
LineupEntry("3",u"blibli",u"blibli",u"blibli",u"blabla",
u"blabla",u"blabla",u"blabla"),
        
LineupEntry("4",u"blabla",u"blabla",u"blabla",u"blabla",
u"blabla",u"blabla",u"blabla"),
        
LineupEntry("5",u"blabla",u"blabla",u"blabla",u"blibli",
u"blibli",u"blibli",u"blibli"),
        
LineupEntry("6",u"blibli",u"blibli",u"blibli",u"blibli",
u"blibli",u"blibli",u"blibli")]
data=mylist0
#---------------------------------------------------
class LineupTable(wx.grid.PyGridTableBase):
    colLabels = (u"Col1",u"Col2",u"Col3",u"Col4",u"Col5",
u"Col6",u"Col7")
    colAttrs=
("one","two","three","four","five","six","seven")
    def __init__(self, entries):
        wx.grid.PyGridTableBase.__init__(self)
        self.entries = entries
        self._rows = self.GetNumberRows()
        self._cols = self.GetNumberCols()

    def GetNumberRows(self):
        return len(self.entries)

    def GetNumberCols(self):
        return 7

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

    def GetRowLabelValue(self, row):#col):
        return self.entries[row].position

    def IsEmptyCell(self, row, col):
        return False

    def GetValue(self, row, col):
        try:
            entry=self.entries[row]
            return getattr(entry,self.colAttrs[col])
        except IndexError:
            return ''
        
    def ResetView(self, grid):
        grid.BeginBatch()
        for current, new, delmsg, addmsg in [
            (self._rows, self.GetNumberRows(),
wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED,
wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED),
            (self._cols, self.GetNumberCols(),
wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED,
wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED)]:

            if new < current:
                msg = wx.grid.GridTableMessage(
self,delmsg,new,current-new)
                grid.ProcessTableMessage(msg)
            elif new > current:
                msg = wx.grid.GridTableMessage(self,addmsg,new-current)
                grid.ProcessTableMessage(msg)
                self.UpdateValues(grid)
        grid.EndBatch()
        self._rows = self.GetNumberRows()
        self._cols = self.GetNumberCols()
        # update the scrollbars and the displayed part of the grid
        grid.AdjustScrollbars()
        grid.ForceRefresh()

    def UpdateValues(self, grid):
        msg = wx.grid.GridTableMessage(self,
wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
        grid.ProcessTableMessage(msg)
#---------------------------------------------------------------------------
class DomGrid(wx.grid.Grid):
    def __init__(self, parent):
        wx.grid.Grid.__init__(self, parent, -1)
        self.tableBase = LineupTable(data)
        self.SetTable(self.tableBase,True)
        self.ForceRefresh()
    def Reset(self):
        self.tableBase.ResetView(self)
        self.tableBase.UpdateValues(self)
#---------------------------------------------------------------------------
class BloodyGrid(wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self,parent,size=(1024,850))
        self.grid=DomGrid(self)
        mychoice=None
        sample=[u"first",u"second"]
        self.mycombo=wx.ComboBox(self,wx.NewId(),value=u"", choices=sample)
        self.Bind(wx.EVT_COMBOBOX, self.OnSelectCombo)

        self.sizer=wx.BoxSizer(wx.VERTICAL)
        self.subSizer=wx.BoxSizer(wx.HORIZONTAL)
        self.subSizer.Add(self.mycombo)
        self.sizer.Add(self.subSizer,0,wx.EXPAND|wx.ALL,border=5)
        self.sizer.Add(self.grid,10,wx.EXPAND|wx.ALL,border=5)
        self.SetSizer(self.sizer)
        self.sizer.Fit(self)
        self.Centre()
    def OnSelectCombo(self,event):
        mychoice = event.GetSelection()
        if mychoice==0:
            newData=mylist0
        elif mychoice==1:
            newData=mylist1
        self.grid.tableBase.entries=newData
        self.subSizer.Layout()
        self.sizer.Layout()
        self.grid.Reset()
        self.grid.ForceRefresh()
#---------------------------------------------------------------------------
class MaFrame(wx.Frame):
    def __init__(self, parent, log):
        wx.Frame.__init__(self, parent, -1, title=u"TEST", size=(1024,768))
        self.parent=parent
        frameSizer=wx.BoxSizer(wx.VERTICAL)
        self.cp = cp =
wx.CollapsiblePane(self,-1,label=u"CP",
style=wx.CP_DEFAULT_STYLE|wx.CP_NO_TLW_RESIZE)
        self.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED,
self.OnPaneChanged, self.cp)
        self.MakePaneContent(pane=self.cp.GetPane())
        frameSizer.Add(wx.StaticLine(self),0,
wx.EXPAND|wx.TOP|wx.BOTTOM, 10)
        frameSizer.Add(self.cp,0,
                       wx.ALIGN_CENTER_VERTICAL)
        frameSizer.Add(wx.StaticLine(self),0,
wx.EXPAND|wx.TOP|wx.BOTTOM, 10)
        self.SetSizer(frameSizer)
        frameSizer.Fit(self)

    def OnPaneChanged(self, evt=None):
        self.Layout()

    def MakePaneContent(self, pane):
        pane=self.cp.GetPane()
        myGrid=BloodyGrid(pane)
        Box = wx.BoxSizer()
        Box.Add(myGrid, 1, wx.EXPAND|wx.ALL, 5)
        pane.SetSizer(Box)
        pane.Fit()
#--------------------------------------------
if __name__ == '__main__':
    import sys
    app = wx.PySimpleApp()
    frame = MaFrame(None, sys.stdout)
    frame.Show(True)
    app.MainLoop()
#---------------------------------------------------------------------------

DomDom wrote:

grid in a collapsible pane

Dominique wrote:

Hello,

I have a collapsible pane which contains a grid (a pygridtablebase).
When the grid data are updated, the size of the grid does not change.
How to resize it according to the new data ?

I can't run your sample because of line wrapping problems (please attach in the future instead of paste) but in general the sizer automatically adjust to the grid's best size. Since the grid's best size is based on col widths and row heights then either your user or you will need to adjust the cols as needed to set them to the desired widths, and then a layout should take care of the rest.

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

Robin,
Attached the sample to test.
To be more precised, I think the object which contains the grid does not resize automatically.
The only solution I found so far is to setup the grid the first time with a size that will be acceptable later on, but it is certainly possible to better layout the programm.

It looks like the CollapsiblePane sets it's own min size when it is expanded or collapsed, and the min size takes precedence over the best size when the sizer does a layout. This works fine for the CP in normal usage, but since you are doing something that changes the best size then it breaks down. Simply changing the CP's minsize to (-1,-1) and then redoing the Frame's layout takes care of the problem. See my changes to your sample attached.

TPbGridResize.py (6.36 KB)

···

----- Original Message ----- Subject: Re: [wxPython-users] Resize of a

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