About the wx.App class

Hi,

I (re)read the Patrick's manual, and the following point
happens to my mind.
It's related to the wx.App class and the lauching of an
application.

A 'normal' wxPython application looks like this:

···

#-----------------------------------------------------------
import wx
#-----------------------------------------------------------
class MyFrame(wx.Frame):
    def __init__(self, ...):
        wx.Frame.__init__(self, ...)
        ...
#-----------------------------------------------------------
class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(...)
        frame.Show(True)
        self.SetTopWindow(frame)
        return True
#-----------------------------------------------------------
def main():
    app = MyApp()
    app.MainLoop()
#-----------------------------------------------------------
if __name__ == "__main__" :
    main()
#------------------------------------------------------

The sub OnInit is automatically called, when an instance of
MyApp is created (before the app.MainLoop() statement)

Now my question:
Why should/may I not define the MyApp class with code like
this.

class MyApp(wx.App):
    def __init__(self):
        wx.App.__init__(self)
        r = self.OnInit()
    def OnInit(self):
        ...
The above code is working, but it lauches (logically) the
app twice.
It presents the advantage of beeing more 'pythonic'.
It looks like other wxPython classes.

In 'normal' code, I also noticed, that when creating a
instance like app = MyApp() but without app.MainLoop()
python/wxPython crashes. I mean a real crash, not the fact
that the main frame is not desplayed. Is this a normal
behaviour?

What exactly launches an application? Class instanciation
or MainLoop()

Should not the wx.App be cleaned up?

Sorry for such stupid questions!

Jean-Michel Fauth, Switzerland

Jean-Michel Fauth wrote:

Hi,

I (re)read the Patrick's manual, and the following point
happens to my mind.
It's related to the wx.App class and the lauching of an
application.

A 'normal' wxPython application looks like this:

[...]

The sub OnInit is automatically called, when an instance of
MyApp is created (before the app.MainLoop() statement)

Now my question:
Why should/may I not define the MyApp class with code like
this.

class MyApp(wx.App):
    def __init__(self):
        wx.App.__init__(self)
        r = self.OnInit()
    def OnInit(self):
        ...
The above code is working, but it lauches (logically) the
app twice.

Because OnInit is called from within wx.App.__init__ so OnInit is called twice creating two instances of your frame.

It presents the advantage of beeing more 'pythonic'.

You don't have to have an __init__ to be Pythonic, there are lots of other cases out there of deriving a class and not overloading the __init__ from the base.

It looks like other wxPython classes.

True.

For the ancient history buffs out there, this is one thing that has been nearly the same since the very first version of wxPython 0.x done in 1994. This was a SWIG-less version that was started by Harri Passenan that I assisted with. It stagnated pretty quickly though since maintenance of something that huge without SWIG was very difficult. Anyway, the method used now to bootstrap the App object and call OnInit is how we figured out how to do it when we converted from just embedding Python in an wxWindows app into being able to have a standalone wxPython app, and was the one thing I carried over from the old project to build upon when I started the new one in 1996.

In 'normal' code, I also noticed, that when creating a
instance like app = MyApp() but without app.MainLoop()
python/wxPython crashes. I mean a real crash, not the fact
that the main frame is not desplayed. Is this a normal
behaviour?

No. I've seen it if you try to do some things before the app is created, but not if it is just that MainLoop is not being called. One thing to watch out for though is even if your app is just a modal dialog you still need to call MainLoop so it can properly cleanup after the dialog. Then it will exit immediately since there are no more top-level windows left.

What exactly launches an application? Class instanciation
or MainLoop()

In most cases the windows are created when their classes are instantiated. Events won't be responded to or dispatched to handlers until MainLoop.

Should not the wx.App be cleaned up?

It is when the app object is deleted. Further cleanup is done when the wxPython.wx module is cleaned up by Python at shutdown.

Sorry for such stupid questions!

No problem.

···

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