FAQ
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.
************************************************************

Search Discussions

  • Gilles Ganault at Mar 31, 2008 at 1:33 pm

    On Mon, 31 Mar 2008 11:01:19 +0100, "Moynes James" wrote:
    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?
  • Mike Driscoll at Mar 31, 2008 at 1:45 pm

    On Mar 31, 8:33 am, Gilles Ganault wrote:
    On Mon, 31 Mar 2008 11:01:19 +0100, "Moynes James"

    wrote:
    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?
    That's not it...check out my other post with one sample solution.

    Mike
  • Mike Driscoll at Mar 31, 2008 at 1:44 pm
    Moynes,
    On Mar 31, 5:01 am, "Moynes James" 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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMar 31, '08 at 10:01a
activeMar 31, '08 at 1:45p
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase