wxPython; adding a grid to a panel

M

Moynes James

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

##################################################
***********************************************************
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.
************************************************************
 
G

Gilles Ganault

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.

I'm learning wxPython as well. Could it be that you're using
wx.PySimpleApp instead of App?
 
M

Mike Driscoll

Moynes,

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
 
M

Mike Driscoll

I'm learning wxPython as well. Could it be that you're using
wx.PySimpleApp instead of App?

That's not it...check out my other post with one sample solution.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top