Search code examples
pythonlayoutwxpythonwxwidgets

Expand child elements of wx.GridBagSizer on Panel on wx.Notebook


I have a simple application with wx.Notebook, it looks like this:

enter image description here

To lay out its child elements, I used wx.GridBagSizer - and it works perfectly. However, I encountered a small but annoying issue. When I expand the window of wx.Notebook, it does not expand all the elements and results in window like this one below:

enter image description here

Which is not what I want actually. How should my modify my code to be able to expand all the child elements when I resize the window? Cheers :)

My code is quite simple:

import wx
import wx.richtext

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='wx.Notebook')
        book = wx.Notebook(self)
        page = MyPage(parent=book)
        book.AddPage(page, 'Page 1')
        book.AddPage(MyPage(book),'Page 2')
        book.AddPage(MyPage(book),'Page 3')
        self.SetSize(wx.Size(320,250))
        self.Centre()

class MyPage(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, size=(0,0))
        # create sizer to add a border

        bagSizer = wx.GridBagSizer(15, 10)

        mainSizer = wx.BoxSizer(wx.VERTICAL)
        label = wx.StaticText(self, label="Text:")

        self.content1 = wx.TextCtrl(self, size=(300, 20))
        self.content2 = wx.richtext.RichTextCtrl(self, size=(300, 100), style=wx.TE_MULTILINE)

        bagSizer.Add(self.content1, pos=(0, 0), flag=wx.TOP | wx.LEFT | wx.BOTTOM, border=5)

        bagSizer.Add(label, pos=(1, 0), flag=wx.TOP | wx.LEFT | wx.BOTTOM, border=5)

        bagSizer.Add(self.content2, pos=(2, 0), flag=wx.TOP | wx.LEFT | wx.BOTTOM | wx.EXPAND, border=5)

        self.SetSizer(bagSizer)

app = wx.App(False)
MyFrame().Show()
app.MainLoop()

Solution

  • You must tell your GridBagSizer which rows and columns are growable. Try:

    bagSizer.AddGrowableRow(2)
    bagSizer.AddGrowableCol(0)
    
    self.SetSizer(bagSizer)