adding a panel to a notebook: followup

Dear wxPythonians,

In response to my inquiry about adding a panel to a notebook when a button event occurs, Robin pointed out that I had 1) used OnInit where I should have used __init__ and 2) tried to add a text control directly to a panel, instead of interposing a sizer. This advice led me to a working solution (thanks, Robin!).

In case anyone has encountered a similar problem, here are working snippets (clicking the button that calls the onClick function adds a new panel with two text controls):

     def onClick(self,evt):
        self.notebook_1.AddPage(MyPanel(self.notebook_1, -1), "hi", -1)

class MyPanel(wx.Panel):
    def __init__(self, parent, log):
        wx.Panel.__init__(self, parent, -1)
        myPanel = wx.Panel(self, -1)
        myTextCtrl = wx.TextCtrl(myPanel, -1, "Dummy text")
        newTextCtrl = wx.TextCtrl(myPanel, -1, "More text")
        mySizer = wx.BoxSizer(wx.HORIZONTAL)
        mySizer.Add(myTextCtrl, 0, 0, 0)
        mySizer.Add(newTextCtrl, 0, 0, 0)
        myPanel.SetAutoLayout(True)
        myPanel.SetSizer(mySizer)
        mySizer.Fit(myPanel)
        mySizer.SetSizeHints(self)
        myPanel.Layout()

I copied the pieces of this code from demos and samples and I'm not sure what all of the lines do, but if I've understood correctly, the procedure is to write an __init__ section that does the following *in order*:

1) Create the panel, the other widgets (two text controls, in this case), and a sizer to hold the other widgets within the panel.

2) Add the other widgets to the sizer.

3) Invoke SetAutoLayout for the panel.

4) Add the sizer to the panel with SetSizer.

5) Fit the sizer to the panel.

6) Set size hints for the sizer.

7) Tell the panel to lay itself out.

Although this works, if any of it is nonetheless wrong, superfluous, or inelegant, I'd be grateful for correction.

Best,

David
djbpitt+python@pitt.edu

Adi Sieker wrote:

Hi David,

Zitat von David J Birnbaum <djbpitt+python@pitt.edu>:

[...]

Also I haven't had a need for the Fit and SetSizeHints calls.

Fit is used to change the size of the window to the minsize calculated by the sizer, and SetSizeHits calls window.SetSizeHints with that minsize value. Since the notebook will be managing the size of the panel they are not needed in this case.

And since we call self.SetAutoLayout(True)

It is not needed any longer (unless you want to turn off/on the auto layout for some reason) as SetSizer now calls SetAutoLayout(True) for you.

we don't need to call Layout() ourself since wxWidgets will do it for us.

There are a few situations where it is needed, but this is not one of them. An example would be when a window is already sized correctly, and there is nothing in the normal sequence of events that will cause it to change size. Since Layout is normally called in the EVT_SIZE handler it won't get called in those cases so you should do it.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!