Re: wxPython; adding a grid to a panel



Moynes,

On Mar 31, 5:01 am, "Moynes James" <James.Moy...@xxxxxx> wrote:
I am a Python newbie hoping for some help with wxPython.

I am trying to build a frame which will display a wx.grid and a number
of other widgets (text controls, static text and buttons). In all the
example scripts I have seen, a gird is added directly to a frame; is it
possible to place a grid in a box sizer or on a wx.Panel so that it can
be arranged with other widgets in the same frame?

In the script below I have added two panels to a frame. I want to
display the grid in one panel (and later on add other widgets to the
other panel), but I cannot get the grid to display properly.

What am I doing wrong?

Thanks in advance for your help,

James

#################################################
import wx
import wx.grid

class TestTable(wx.grid.PyGridTableBase):
def __init__(self):
wx.grid.PyGridTableBase.__init__(self)
self.rowLabels = ["uno", "dos", "tres", "quatro", "cinco"]
self.colLabels = ["homer", "marge", "bart", "lisa", "maggie"]

def GetNumberRows(self):
return 5

def GetNumberCols(self):
return 5

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

def GetValue(self, row, col):
return "(%s,%s)" % (self.rowLabels[row], self.colLabels[col])

def SetValue(self, row, col, value):
pass

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

def GetRowLabelValue(self, row):
return self.rowLabels[row]

class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Grid Table",
size=(500,200))
panel1 = wx.Panel(self, -1)
panel2 = wx.Panel(self, -1)
hbox1 = wx.BoxSizer(wx.HORIZONTAL)

hbox1.Add(panel1, 1, wx.EXPAND | wx.ALL, 3)
hbox1.Add(panel2, 1, wx.EXPAND | wx.ALL, 3)

grid = wx.grid.Grid(panel2, wx.EXPAND)
table = TestTable()
grid.SetTable(table, True)
self.SetSizer(hbox1)

app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()

##################################################

I'd never used the PyGridTableBase to do this before, so I used the
wxPython demo to figure it out. If you check out the GridCustomTable
demo code, you'd probably have gotten it as well. Basically, you
needed to add another class that inherited from wx.grid.Grid that
would use the base you created. Here's the modified code:

<code>

import wx
import wx.grid as gridlib

class TestTable(wx.grid.PyGridTableBase):
def __init__(self):
gridlib.PyGridTableBase.__init__(self)
self.rowLabels = ["uno", "dos", "tres", "quatro", "cinco"]
self.colLabels = ["homer", "marge", "bart", "lisa", "maggie"]


def GetNumberRows(self):
return 5

def GetNumberCols(self):
return 5

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

def GetValue(self, row, col):
return "(%s,%s)" % (self.rowLabels[row], self.colLabels[col])

def SetValue(self, row, col, value):
pass

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

def GetRowLabelValue(self, row):
return self.rowLabels[row]

class CustTableGrid(gridlib.Grid):
def __init__(self, parent):
gridlib.Grid.__init__(self, parent, -1)

table = TestTable()

# The second parameter means that the grid is to take
ownership of the
# table and will destroy it when done. Otherwise you would
need to keep
# a reference to it and call it's Destroy method later.
self.SetTable(table, True)

self.SetRowLabelSize(0)
self.SetMargins(0,0)
self.AutoSizeColumns(False)

class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Grid Table",
size=(500,200))
panel1 = wx.Panel(self, -1)
panel2 = wx.Panel(self, -1)
hbox1 = wx.BoxSizer(wx.HORIZONTAL)

hbox1.Add(panel1, 1, wx.EXPAND | wx.ALL, 3)
hbox1.Add(panel2, 1, wx.EXPAND | wx.ALL, 3)


table = CustTableGrid(panel2)
tblSizer = wx.BoxSizer(wx.VERTICAL)
tblSizer.Add(table, 1, wx.ALL|wx.EXPAND, 5)
panel2.SetSizer(tblSizer)

self.SetSizer(hbox1)

app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()

</code>

FYI: There's an excellent wxPython mailing list - http://wxpython.org/maillist.php

Mike
.


Quantcast