Question

When I embed a wx.Panel in another wx.Panel (here ChildPanel is embedded in MyPanel), by default this ChildPanel doesn't use the full size of the parent. Why?

Is there a trick for saying that ChildPanel should use full space of MyPanel ? Is it possible without a BoxSizer ?

import wx

class MyPanel(wx.Panel):
   def __init__(self, *args, **kwargs):
        wx.Panel.__init__(self, *args, **kwargs)
        self.SetBackgroundColour('#00f8f8')

        # ChildPanel
        self.ChildPanel = wx.Panel(self)
        self.ChildPanel.SetBackgroundColour('#000000')

class DrawFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        MyPanel(self)
        self.Show()        

app = wx.App(False)
F = DrawFrame(None, title="Test", size=(500,500))
app.MainLoop() 
Was it helpful?

Solution

wxFrame is a special case and is the only window which resizes its only child to entirely fill up its client area. Other windows, including wxPanel, don't do this, so you do need to use a sizer -- or position the child panel manually in wxEVT_SIZE handler.

Of course, if you think about it, it makes sense: while it's pretty common to have a wxPanel entirely covering up the wxFrame, it's very rare to have a wxPanel or wxWindow as the only child of another wxPanel because it simply doesn't make any sense to do it, you're just wasting a window for nothing (and windows are a relatively heavy resource, so you shouldn't do this).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top