[wxPython-users] XRC and dynamically created widgets?
Robin Dunn
robin at alldunn.com
Mon Oct 2 10:49:17 PDT 2006
John Salerno wrote:
> Ok, I think I asked this question more generally a few days ago, but now
> that I have some XRC experience, I will be more specific and hopefully
> it will make more sense.
>
> But before I ask, I have one other question. In an example on the
> wxPyWiki I saw this kind of code:
>
> def InitFrame(self):
> 1 self.frame = self.res.LoadFrame(None, "MainFrame")
> 2 self.panel = xrc.XRCCTRL(self.frame, "MainPanel")
> 3 self.first_arg = xrc.XRCCTRL(self.panel, "FirstArg")
> 4 self.second_arg = xrc.XRCCTRL(self.panel, "SecondArg")
> 5 self.result = xrc.XRCCTRL(self.panel, "Result")
>
>
> Are lines 2-5 necessary for creating the widgets, or are they there just
> so the program will have access to them later? I ran something similar
> myself and it seems like the LoadFrame call automatically creates and
> shows the children of the frame, so I wasn't sure if 2-5 were actually
> doing anything at this point, or if they were just to assign references.
They are just for assigning references. The XRCCTRL function basically
looks up the ID for the name, and then does a FindWindowById.
>
> Ok, now my main question: I want to create a program that will allow the
> user to open new tabs using the Notebook widget. I understand how to use
> XRC to design a static layout, but I don't understand where the GUI code
> is supposed to go for the notebook pages that will be created during the
> run of the program. It's not something that I want to appear when the
> program starts, so it seems like it's wrong to put them as children of
> the frame. I thought maybe, since notebook pages are custom Panels, that
> I could put a top-level Panel widget in the XRC file, but I tried this
> and it didn't seem to work. I wasn't sure how to access it, and when the
> program finally ran without errors, it just showed a frame with a panel.
>
> So I hope someone can explain how to use XRC when you expect to create
> new widgets during runtime.
If you define the pages in the XRC as children of the notebook then when
you load the notebook it will load all the pages. If you want to load
the pages separately, then define them in the XRC as separate top-level
panels. Then in your code when you decide to add a page you can load it
from the XRC at that time and add it to the notebook programatically.
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!
More information about the wxpython-users
mailing list