Skip to content Skip to sidebar Skip to footer

Wxpython: Displaying Multiple Widgets In Same Frame

I would like to be able to display Notebook and a TxtCtrl wx widgets in a single frame. Below is an example adapted from the wxpython wiki; is it possible to change their layout (

Solution 1:

Making two widgets appear on the same frame is easy, actually. You should use sizers to accomplish this.

In your example, you can change your Notebook class implementation to something like this:

classNotebook(wx.Frame):
    def__init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(600, 600))
        menubar = wx.MenuBar()
        file = wx.Menu()
        file.Append(101, 'Quit', '' )
        menubar.Append(file, "&File")
        self.SetMenuBar(menubar)
        wx.EVT_MENU(self, 101, self.OnQuit)
        nb = wx.Notebook(self, -1, style=wx.NB_BOTTOM)
        self.sheet1 = MySheet(nb)
        self.sheet2 = MySheet(nb)
        self.sheet3 = MySheet(nb)
        nb.AddPage(self.sheet1, "Sheet1")
        nb.AddPage(self.sheet2, "Sheet2")
        nb.AddPage(self.sheet3, "Sheet3")
        self.sheet1.SetFocus()
        self.StatusBar()
        # new code begins here:# add your text ctrl:
        self.text = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE)
        # create a new sizer for both controls:
        sizer = wx.BoxSizer(wx.VERTICAL)
        # add notebook first, with size factor 2:
        sizer.Add(nb, 2)
        # then text, size factor 1, maximized
        sizer.Add(self.text, 1, wx.EXPAND)
        # assign the sizer to Frame:
        self.SetSizerAndFit(sizer)

Only the __init__ method is changed. Note that you can manipulate the proportions between the notebook and text control by changing the second argument of the Add method.

You can learn more about sizers from the official Sizer overview article.

Solution 2:

You can use a splitter, yes.

Also, it makes sense to create a Panel, place your widgets in it (with sizers), and add this panel to the Frame.

Post a Comment for "Wxpython: Displaying Multiple Widgets In Same Frame"