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()
#---------------------------------------------------------------------------