Very basic question about grids

I have a decent knowledge of python and I'm somewhat new to
wxwindows/wxpython.

I don't understand how the display of widgets are controlled. I took a
simple example from the demo docs and edited it so I would have a better
chance of understanding what controls the display.

In the example, I created a small grid and calendar. If I leave both in,
the calendar is displayed and the grid is not displayed; if I comment
either out, the other is displayed. I'm not trying to create anything
useful, just understand the underlying mechanism.

Without using too much of your time, would it be possible for someone to
shed some light on the matter? The example is appended below.

Thanks,

gary

from wxPython.wx import *
from wxPython.calendar import *
from wxPython.grid import *

class BasicGrid(wxGrid):
    def __init__(self, parent):
        wxGrid.__init__(self, parent, -1)
        self.moveTo = None
        self.CreateGrid(4, 4)
        self.SetCellValue(0, 0, "First cell")
        self.SetCellValue(1, 1, "Another Cell")
        EVT_IDLE(self, self.OnIdle)

    def OnIdle(self, evt):
        if self.moveTo != None:
            self.SetGridCursor(self.moveTo[0], self.moveTo[1])
            self.moveTo = None
        evt.Skip()

class MyFrame(wxFrame):
    def __init__(self, parent, ID, title):
        wxFrame.__init__(self, parent, ID, title,
        wxDefaultPosition, wxSize(800, 800))
        grid = BasicGrid(self)
        cal = wxCalendarCtrl(self, -1, wxDateTime_Now(),
                             pos = (25,50),
                             style = wxCAL_SHOW_HOLIDAYS | wxCAL_SUNDAY_FIRST)

class MyApp(wxApp):
    def OnInit(self):
        frame = MyFrame(NULL, -1, "Example")
        frame.Show(true)
        self.SetTopWindow(frame)
        return true

app = MyApp(0)
app.MainLoop()

gh@rattler.cameron.edu wrote:

In the example, I created a small grid and calendar. If I leave both in,
the calendar is displayed and the grid is not displayed; if I comment
either out, the other is displayed. I'm not trying to create anything
useful, just understand the underlying mechanism.

By default, a frame with a single child just lets the child take up the entire frame. With more than one control in the frame, you need some way of adjusting their position and size so they don't overlap. Look up sizers in the documentation (and in recent posts in the mailing list).

David

You're best to use sizers to layout your widgets - see recent posts for good links to explain sizers.

The problem with grids (if I remember correctly) is that they are created with a default size of (0,0), so can't be seen.

See changes below:

···

__________________________

from wxPython.wx import *
from wxPython.calendar import *
from wxPython.grid import *

class BasicGrid(wxGrid):
    def __init__(self, parent):
        wxGrid.__init__(self, parent, -1, size = wxSize(400,100))
        self.moveTo = None
        self.CreateGrid(4, 4)
        self.SetCellValue(0, 0, "First cell")
        self.SetCellValue(1, 1, "Another Cell")
        EVT_IDLE(self, self.OnIdle)

    def OnIdle(self, evt):
        if self.moveTo != None:
            self.SetGridCursor(self.moveTo[0], self.moveTo[1])
            self.moveTo = None
        evt.Skip()

class MyFrame(wxFrame):
    def __init__(self, parent, ID, title):
        wxFrame.__init__(self, parent, ID, title,
        wxDefaultPosition, wxSize(800, 800))
        grid = BasicGrid(self)
        cal = wxCalendarCtrl(self, -1, wxDateTime_Now(),
                             pos = (25,50),
                             style = wxCAL_SHOW_HOLIDAYS | wxCAL_SUNDAY_FIRST)
        sizer_1 = wxBoxSizer(wxHORIZONTAL)
        sizer_1.Add(grid, 1, wxEXPAND, 0)
        sizer_1.Add(cal, 0, wxALIGN_CENTER_VERTICAL, 0)
        self.SetAutoLayout(1)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        #~ sizer_1.SetSizeHints(self)

class MyApp(wxApp):
    def OnInit(self):
        frame = MyFrame(NULL, -1, "Example")
        frame.Show(true)
        self.SetTopWindow(frame)
        return true

app = MyApp(0)
app.MainLoop()