two step creation - why wait for the WINDOW_CREATE event

Hi,
ca anybody tell me what exactly is happening between PostCreate and the
creation of the window in case I let the xrc processor create my window
instances?

class Panel(wx.Panel)
    def __init__(self, parent):
        pre = wx.PrePanel()
        # the Create step is done by XRC.
        self.PostCreate(pre)
        self.Bind(wx.EVT_WINDOW_CREATE, self.OnCreate)

    def OnCreate(self, event):
        self.Unbind(wx.EVT_WINDOW_CREATE)
        wx.CallAfter(self._PostInit)
        event.Skip()
        return True

    def _PostInit(self):
        etc.

Is there anything I am not allowed to do before the window create evt has been
sent? And does it make any difference if I create the instances my self like

    def __init__(self, parent):
        pre = wx.PrePanel()
        self.res = xrc.XmlResource.Get()
        self.res.LoadOnPanel(pre, parent, "thepanel")
        self.PostCreate(pre)
        etc.

Regards, Christian

Christian Kristukat wrote:

Is there anything I am not allowed to do before the window create evt has been
sent?

Anything that requires a real UI portion of the object to have been created already, like creating a DC for it, etc. In some cases things you might expect to be a problem, like SetLabel or Show, are handled in the wxWidgets layers and are automatically deferred until the create.

And does it make any difference if I create the instances my self like

    def __init__(self, parent):
        pre = wx.PrePanel()
        self.res = xrc.XmlResource.Get()
        self.res.LoadOnPanel(pre, parent, "thepanel")
        self.PostCreate(pre)
        etc.

No, it is essentially the same.

···

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

Robin Dunn <robin <at> alldunn.com> writes:

Christian Kristukat wrote:

>
> Is there anything I am not allowed to do before the window create evt has
> been
> sent?

Anything that requires a real UI portion of the object to have been
created already, like creating a DC for it, etc. In some cases things
you might expect to be a problem, like SetLabel or Show, are handled in
the wxWidgets layers and are automatically deferred until the create.

I'm asking because while doing a mayor cleanup and refactoring according to mvc
patterns of my code I tried to remove the WINDOW_CREATE handler and call
PostInit directly (without CallAfter) after the call to PostCreate and
surprisingly (or maybe not?) everything works fine. In the PostInit part I'm
basically adding custom controls to open nodes of the xrc tree. Are those
operations 'allowed' before the window is created? Btw. I remeber that for some
controls at least on windows the WINDOW_CREATE event was never sent (e.g.
TreeCtrl) which makes me think that is maybe not that important to wait for?

Christian

Christian Kristukat wrote:

Robin Dunn <robin <at> alldunn.com> writes:

Christian Kristukat wrote:

Is there anything I am not allowed to do before the window create evt has
been
sent?

Anything that requires a real UI portion of the object to have been created already, like creating a DC for it, etc. In some cases things you might expect to be a problem, like SetLabel or Show, are handled in the wxWidgets layers and are automatically deferred until the create.

I'm asking because while doing a mayor cleanup and refactoring according to mvc
patterns of my code I tried to remove the WINDOW_CREATE handler and call
PostInit directly (without CallAfter) after the call to PostCreate and
surprisingly (or maybe not?) everything works fine. In the PostInit part I'm
basically adding custom controls to open nodes of the xrc tree. Are those
operations 'allowed' before the window is created?

If it works, then yes. :wink: Be sure to test well on all the platforms you want to deploy to as this is one area where platform differences can easily jump up and bite you.

Btw. I remeber that for some
controls at least on windows the WINDOW_CREATE event was never sent (e.g.
TreeCtrl) which makes me think that is maybe not that important to wait for?

Since the native controls on Windows are not "subclassed" (in the win32 API sense of that word) until after they are created, then the window create message is lost.

···

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

Robin Dunn <robin <at> alldunn.com> writes:

Christian Kristukat wrote:
> Robin Dunn <robin <at> alldunn.com> writes:
>
>> Christian Kristukat wrote:
>>
>>> Is there anything I am not allowed to do before the window create evt has
>>> been
>>> sent?
>> Anything that requires a real UI portion of the object to have been
>> created already, like creating a DC for it, etc. In some cases things
>> you might expect to be a problem, like SetLabel or Show, are handled in
>> the wxWidgets layers and are automatically deferred until the create.
>
> I'm asking because while doing a mayor cleanup and refactoring according to
> mvc
> patterns of my code I tried to remove the WINDOW_CREATE handler and call
> PostInit directly (without CallAfter) after the call to PostCreate and
> surprisingly (or maybe not?) everything works fine. In the PostInit part I'm
> basically adding custom controls to open nodes of the xrc tree. Are those
> operations 'allowed' before the window is created?

If it works, then yes. Be sure to test well on all the platforms
you want to deploy to as this is one area where platform differences can
easily jump up and bite you.

Oh, nice! At least on windows XP and linux with wxPython 2.6.3 and 2.8.1
it works.
In fact I never liked it very much to be obliged to wait for the
window creation event. I found it quite annoying that even though the main loop
has already started I could not reliably access any wx control. I remember that
I once noticed that the window creation event of a notebook tab was not sent
until the tab was selected for the first time and so I could not access
panel class members which were set in the _PostInit method. Good to know that
you can do many (all) things before the event is fired.
Is it possible that future versions of wxPython will not as good-natured? And
in that case, has anybody an elegant way to cope with that situation?

Regards, Christian