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