wxPython; adding a grid to a panel

Moynes James James.Moynes at rte.ie
Mon Mar 31 06:01:19 EDT 2008


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

##################################################
-------------- next part --------------
***********************************************************
The information in this e-mail is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this e-mail by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in reliance
on it, is prohibited and may be unlawful.
Please note that emails to, from and within RT? may be subject to the Freedom
of Information Act 1997 and may be liable to disclosure.
************************************************************


More information about the Python-list mailing list