Hello,
I’m trying to write a simple grid function. Please see code below. My question is: is there a way to let the panel to auto-size itself to fit the grid so that user does not need to drag to frame to see all the cells?
Thanks for your help!
import wx
import wx.grid
class GridBaseResult(object):
def init(self):
self.value = None
class GridBaseFrame(wx.Frame):
def init(self, lst, data, title, rowLabels = None, colLabels = None,
msg = "", hAlign = wx.ALIGN_CENTRE, vAlign = wx.ALIGN_CENTRE):
wx.Frame.__init__(self, None, -1, title, pos = (300, 200))
panel = wx.Panel(self)
self.data = data
msgLbl = wx.StaticText(panel, -1, msg)
topSizer = wx.BoxSizer(wx.HORIZONTAL)
topSizer.Add (msgLbl,0)
self.okBtn = wx.Button(panel, -1, "Ok")
self.cancelBtn = wx.Button(panel, -1, "Cancel")
btnSizer = wx.BoxSizer(wx.VERTICAL)
btnSizer.Add(self.okBtn, 0)
btnSizer.Add((10,10), 0)
btnSizer.Add(self.cancelBtn, 0)
self.Bind(wx.EVT_BUTTON, self.OnOkClick, self.okBtn)
self.Bind(wx.EVT_BUTTON , self.OnCancelClick, self.cancelBtn)
cntRow = len(lst)
cntCol = len(lst[0])
self.grid = wx.grid.Grid(panel)
self.grid.CreateGrid(cntRow,cntCol)
botSizer = wx.BoxSizer(wx.HORIZONTAL)
botSizer.Add (self.grid, 1, wx.GROW|wx.ALL, 10)
botSizer.Add
(btnSizer, 0, wx.EXPAND|wx.ALL, 10)
mainSizer = wx.BoxSizer (wx.VERTICAL)
mainSizer.Add(topSizer, 0, wx.GROW|wx.ALL, 10)
mainSizer.Add(botSizer, 1, wx.EXPAND|wx.ALL, 10)
panel.SetSizer
(mainSizer)
mainSizer.Fit (self)
mainSizer.SetSizeHints(self)
if rowLabels:
for row in range(cntRow):
self.grid.SetRowLabelValue(row, rowLabels[row])
if colLabels:
for col in range(cntCol):
self.grid.SetColLabelValue(col, colLabels[col])
for row in range(cntRow):
for col in range(cntCol):
self.grid.SetCellValue(row, col, lst[row][col])
self.grid.SetCellAlignment(row, col, hAlign, vAlign)
def OnOkClick(self, event):
self.data.value
= self.GetGridValues(self.grid)
self.Destroy()
def OnCancelClick(self, event):
self.data.value = None
self.Destroy()
def GetGridValues (self, grid):
cntRow = grid.GetNumberRows()
cntCol = grid.GetNumberCols()
lst = [[]]*cntRow
for row in range(cntRow):
for col in range(cntCol):
lst[row].append(grid.GetCellValue
(row,col))
return lst
def GridBase (title, lst, rowLabels = None, colLabels = None,
msg = “”, hAlign = wx.ALIGN_CENTRE, vAlign = wx.ALIGN_CENTRE):
app = wx.PySimpleApp
()
data = GridBaseResult()
GridBaseFrame(lst, data, title, rowLabels, colLabels, msg, hAlign, vAlign).Show()
app.MainLoop()
return data.value
def _Test_GridBase():
title = “Simple Grid”
lst = [["00", "01", "02"], ["10", "11", "12"], ["20", "21", "22"], ["30", "31", "32"]]
rowLabels = ['row1', 'row2', 'row3','row4']
colLabels = ['col1', 'col2', 'col3']
msg = "Edit data in grid"
lst = GridBase(title, lst, rowLabels, colLabels, msg)
print lst
if name == “main”:
_Test_GridBase()
···
–
- wcc