[wxPython-users] Resize of a grid in a collapsible pane

Robin Dunn robin at alldunn.com
Wed Aug 22 17:07:33 PDT 2007


DomDom wrote:
> =

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

> 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.

-- =

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

-------------- next part --------------
# -*- 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=3Dposition
        self.one=3Done
        self.two=3Dtwo
        self.three=3Dthree
        self.four=3Dfour
        self.five=3Dfive
        self.six=3Dsix
        self.seven=3Dseven

mylist0=3D[
    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=3D[
    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=3Dmylist0

#---------------------------------------------------
class LineupTable(wx.grid.PyGridTableBase):
    colLabels =3D (u"Col1",u"Col2",u"Col3",u"Col4",u"Col5",u"Col6",u"Col7")
    colAttrs=3D("one","two","three","four","five","six","seven")
    def __init__(self, entries):
        wx.grid.PyGridTableBase.__init__(self)
        self.entries =3D entries
        self._rows =3D self.GetNumberRows()
        self._cols =3D 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=3Dself.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 =3D wx.grid.GridTableMessage(self,delmsg,new,current-ne=
w)
                grid.ProcessTableMessage(msg)
            elif new > current:
                msg =3D wx.grid.GridTableMessage(self,addmsg,new-current)
                grid.ProcessTableMessage(msg)
                self.UpdateValues(grid)
        grid.EndBatch()
        self._rows =3D self.GetNumberRows()
        self._cols =3D self.GetNumberCols()
        # update the scrollbars and the displayed part of the grid
        grid.AdjustScrollbars()
        grid.ForceRefresh()

    def UpdateValues(self, grid):
        msg =3D wx.grid.GridTableMessage(self,wx.grid.GRIDTABLE_REQUEST_VIE=
W_GET_VALUES)
        grid.ProcessTableMessage(msg)
#---------------------------------------------------------------------------
class DomGrid(wx.grid.Grid):
    def __init__(self, parent):
        wx.grid.Grid.__init__(self, parent, -1)
        self.tableBase =3D 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, frame):
        wx.Panel.__init__(self,parent,size=3D(1024,850))
        self.frame =3D frame
        self.grid=3DDomGrid(self)
        mychoice=3DNone
        sample=3D[u"first",u"second"]
        self.mycombo=3Dwx.ComboBox(self,wx.NewId(),value=3Du"", choices=3Ds=
ample)
        self.Bind(wx.EVT_COMBOBOX, self.OnSelectCombo)

        self.sizer=3Dwx.BoxSizer(wx.VERTICAL)
        self.subSizer=3Dwx.BoxSizer(wx.HORIZONTAL)
        self.subSizer.Add(self.mycombo)
        self.sizer.Add(self.subSizer,0,wx.EXPAND|wx.ALL,border=3D5)
        self.sizer.Add(self.grid,10,wx.EXPAND|wx.ALL,border=3D5)
        self.SetSizer(self.sizer)
        self.sizer.Fit(self)
        self.Centre()
        =

    def OnSelectCombo(self,event):
        mychoice =3D event.GetSelection()
        if mychoice=3D=3D0:
            newData=3Dmylist0
        elif mychoice=3D=3D1:
            newData=3Dmylist1

        self.grid.tableBase.entries=3DnewData
        self.grid.Reset()
        self.grid.ForceRefresh()

        self.frame.ResetLayout()

        =

#---------------------------------------------------------------------------
class MaFrame(wx.Frame):
    def __init__(self, parent, log):
        wx.Frame.__init__(self, parent, -1, title=3Du"TEST", size=3D(1024,7=
68))
        self.parent=3Dparent
        frameSizer=3Dwx.BoxSizer(wx.VERTICAL)
        self.cp =3D cp =3Dwx.CollapsiblePane(self,-1,label=3Du"CP",style=3D=
wx.CP_DEFAULT_STYLE|wx.CP_NO_TLW_RESIZE)
        self.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED,self.OnPaneChanged, self.c=
p)
        self.MakePaneContent(pane=3Dself.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=3DNone):
        print "pane changed"
        self.Layout()

    def MakePaneContent(self, pane):
        pane=3Dself.cp.GetPane()
        myGrid=3DBloodyGrid(pane, self)
        Box =3D wx.BoxSizer()
        Box.Add(myGrid, 1, wx.EXPAND|wx.ALL, 5)
        pane.SetSizer(Box)
        pane.Fit()

    def ResetLayout(self):
        self.cp.SetMinSize((-1,-1))
        self.Layout()
        =

#---------------------------------------------------------------------------
if __name__ =3D=3D '__main__':
    import sys
    import wx.lib.mixins.inspection
    app =3D wx.lib.mixins.inspection.InspectableApp(False)
    app.Init()
    frame =3D MaFrame(None, sys.stdout)
    frame.Show(True)
    app.MainLoop()
#---------------------------------------------------------------------------



More information about the wxpython-users mailing list