About the wx.App class

Thanks Robin
1) About the app.MainLoop()

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.

I do not agree with you, if I'm considering the following
basic
application:

···

#-----------------------------------------------------------
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()
#------------------------------------------------------

With the "app.MainLoop()", it works fine. But if I remove
the
app.MainLoop() line, I got the following:
i) the main frame flashes on the screen (it appears and
desappears), this is
normal
ii) but I got the following message:
21:15:36: Debug: e:\projects\wx\src\msw\app.cpp(439):
'UnregisterClass(canvas)' failed with error 0x00000000
(l'op�ration s'est termin�e.).
- it is not a dialog window
- I do not try to do some things before the app is created
- The problem remains, if I put the two lines
app = MyApp()
app.MainLoop()
directly in the if __name__ == ... block
(win98, py2.2.3, wxpy 2.4.0.1)

2)

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 my english, cleanup was not the correct word.
I was asking, if it is not better to disconnect the OnInit()
from
the MyApp class instantiation I mean, forcing the programmer
to launch/bootstrap the application by a the statement like
OnInit
in the __init__ of MyApp.

class MyApp(wx.App):
    def __init__(self):
        wx.App.__init__(self)
        r = self.OnInit() #Force here the start of the
application
    def OnInit(self):
        ...
- Code is nicer, it looks like others wx classes.
- Possibility to put some code in the __init__ instead
of in the main() sub, like db initialisation
- You told, the bootstrap is an old stuff

Regards,
Jean-Michel Fauth, Switzerland

Jean-Michel Fauth wrote:

With the "app.MainLoop()", it works fine. But if I remove
the
app.MainLoop() line, I got the following:
i) the main frame flashes on the screen (it appears and
desappears), this is
normal
ii) but I got the following message:
21:15:36: Debug: e:\projects\wx\src\msw\app.cpp(439):
'UnregisterClass(canvas)' failed with error 0x00000000
(l'opération s'est terminée.).

Well, that's not a crash. It's just a log message reporting an error from a windows API function. The problem is that you have created a frame and are trying to shutdown the program while it still exists.

Since you don't call MainLoop control falls off the end of your script and so Python cleans up and exits, during which time wxPython cleans up after itself and then the above message is generated.

Sorry for my english, cleanup was not the correct word.
I was asking, if it is not better to disconnect the OnInit()
from
the MyApp class instantiation I mean, forcing the programmer
to launch/bootstrap the application by a the statement like
OnInit
in the __init__ of MyApp.

class MyApp(wx.App):
    def __init__(self):
        wx.App.__init__(self)
        r = self.OnInit() #Force here the start of the
application
    def OnInit(self):
        ...
- Code is nicer, it looks like others wx classes.

Take a look at __init__, you'll see that OnInit is not called directly, but from C++. This is not easy to change.

         # this initializes wxWindows and then calls our OnInit
         _wxStart(self.OnInit)

- Possibility to put some code in the __init__ instead
of in the main() sub, like db initialisation

You still can override __init__ of course and still do all this. Or put that code in OnInit. Or not override OnInit at all and just use wxPySimpleApp and then create your frame and db connections and whatever after the app has been created.

- You told, the bootstrap is an old stuff

Being old does not mean that it needs to change. It means that it is stable and works good enough that it has not needed to change for a long time.

···

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