What's up with OnInit() ?

Hello,

What's up with the OnInit method? When do I need it? On the wiki I see two different ways to start a wxPython program:

class MyApp(wx.Frame):
     def __init__(self):
         wx.Frame.__init__(self, None, wx.ID_ANY, "Hello")

and

class MyApp(wx.App):
     def OnInit(self):

What's the difference between these? Why are there two?

Daniel.

Hi Daniel,

Hello,

What's up with the OnInit method? When do I need it? On the wiki I see
two different ways to start a wxPython program:

class MyApp(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Hello")

and

class MyApp(wx.App):
def OnInit(self):

What's the difference between these? Why are there two?

Daniel.

It's probably just inherited from the C++ stuff, but you'll note that
OnInit() only applies to wx.App classes. Here's what the wxPython in
Action book says:

<quote>

It’s called by the wxPython system when the application is started and
before the main event loop begins. This method takes no parameters and
returns a boolean value—if the return value is False, then the
application will exit immediately. In most cases, you’ll want to
hardwire True as the result of this method. Exiting might be the
proper way to handle certain error conditions, such as the absence of
a required resource. Because the OnInit() method exists, and is part
of the wxPython framework, any initialization needed for your custom
class is typically managed there, and not in the Python __init__
special method. If you decide that you need an __init__ method for
some reason, you must call the __init__ method of the parent class in
that method, as in the following.
wx.App.__init__(self)

Typically, you’ll create at least one frame object within the OnInit()
method, and you’ll also call the Show() method of that frame. You may
optionally specify that the frame is the top window for the
application by calling the method SetTopWindow(). The top window is
used as the default parent for dialogs that are created without a
parent—it’s essentially the main window of your program. We’ll discuss
the top-level window in section 2.5.

</quote>

Hopefully Manning and Dunn won't hunt me down now...

···

On Jan 14, 1:11 pm, Daniel Carrera <dcarr...@gmail.com> wrote:

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

PyCon 2010 Atlanta Feb 19-21 http://us.pycon.org/

Mike Driscoll wrote:

What's the difference between these? Why are there two?

Daniel.

It's probably just inherited from the C++ stuff, but you'll note that
OnInit() only applies to wx.App classes. Here's what the wxPython in
Action book says:

Thanks. It sounds like the OnInit() method does important things. Should that be the standard way to write wxPython apps? I notice that the Getting Started page doesn't use this method.

What do you use in your own programs? wx.Frame with __init__ or wx.App with OnInit() ? What is wx.App anyways?

Thanks for the help.

Daniel.

Mike Driscoll wrote:

What's the difference between these? Why are there two?

Daniel.

It's probably just inherited from the C++ stuff, but you'll note that
OnInit() only applies to wx.App classes. Here's what the wxPython in
Action book says:

Thanks. It sounds like the OnInit() method does important things.

In C++ OnInit is usually the place that the application program hooks its startup code to the wx framework. It is meant to be the place that the initial gui objects are created and shown.

Should
that be the standard way to write wxPython apps? I notice that the
Getting Started page doesn't use this method.

For a long time wxPython followed the same pattern as the C++ side of things ang wxPython apps needed to use OnInit. Over time we were able to streamline things somewhat for Python and now hooking in to the wx framework at OnInit is not necessary (although some will still say that a well designed application will still do it that way.) Now something like this can be the minimal application:

import wx
app = wx.App(redirect=False)
frm = wx.Frame(None, title="Hello World")
frm.Show()
app.MainLoop()

But more advanced applications usually will derive a class from wx.App and override OnInit because of some of the other benefits it provides, and also because of OOP design principles.

What do you use in your own programs? wx.Frame with __init__ or wx.App
with OnInit() ? What is wx.App anyways?

Creating an instance of wx.App (either directly or a derived class with OnInit overridden) is required and must be one of the first things done in a wx application. It is where the UI toolkit is initialized and other important things are done, and you will not be able to have a functional wx application without a wx.App object. It is also the entity that runs and manages the main event loop, and there are other whole application services that it provides.

···

On 1/14/10 12:00 PM, Daniel Carrera wrote:

--
Robin Dunn
Software Craftsman

Robin Dunn wrote:

For a long time wxPython followed the same pattern as the C++ side of things ang wxPython apps needed to use OnInit. Over time we were able to streamline things somewhat for Python and now hooking in to the wx framework at OnInit is not necessary (although some will still say that a well designed application will still do it that way.) Now something like this can be the minimal application:

import wx
app = wx.App(redirect=False)
frm = wx.Frame(None, title="Hello World")
frm.Show()
app.MainLoop()

But more advanced applications usually will derive a class from wx.App and override OnInit because of some of the other benefits it provides, and also because of OOP design principles.

Ok. I see. Simple apps can get away with the standard wx.App() but complex apps may need to modify it and that's when OnInit() comes in. Makes sense now.

What does wx.PySimpleApp() do that's different from wx.App() ?

I think that the issue with OnInit() will be easier to explain if the new user has been typing wx.App() from the beginning. So when you introduce more complex programs you can say "you know that wx.App() you've been typing? To do the following task we're going to extend that class".

Creating an instance of wx.App (either directly or a derived class with OnInit overridden) is required and must be one of the first things done in a wx application. It is where the UI toolkit is initialized and other important things are done, and you will not be able to have a functional wx application without a wx.App object. It is also the entity that runs and manages the main event loop, and there are other whole application services that it provides.

Yeah, it all makes sense. Thanks. I hadn't realized that I had been using wx.App() all along, disguised as wx.PySimpleApp().

Daniel.

Since the streamlining changes mentioned before they are essentially the same. I think the only real difference is that the redirect parameter defaults to False in PySimpleApp on every platform. In 2.9 wx.App will do that too.

···

On 1/14/10 12:41 PM, Daniel Carrera wrote:

Robin Dunn wrote:

For a long time wxPython followed the same pattern as the C++ side of
things ang wxPython apps needed to use OnInit. Over time we were able
to streamline things somewhat for Python and now hooking in to the wx
framework at OnInit is not necessary (although some will still say
that a well designed application will still do it that way.) Now
something like this can be the minimal application:

import wx
app = wx.App(redirect=False)
frm = wx.Frame(None, title="Hello World")
frm.Show()
app.MainLoop()

But more advanced applications usually will derive a class from wx.App
and override OnInit because of some of the other benefits it provides,
and also because of OOP design principles.

Ok. I see. Simple apps can get away with the standard wx.App() but
complex apps may need to modify it and that's when OnInit() comes in.
Makes sense now.

What does wx.PySimpleApp() do that's different from wx.App() ?

--
Robin Dunn
Software Craftsman

Robin Dunn wrote:

What does wx.PySimpleApp() do that's different from wx.App() ?

Since the streamlining changes mentioned before they are essentially the same. I think the only real difference is that the redirect parameter defaults to False in PySimpleApp on every platform. In 2.9 wx.App will do that too.

May I edit the Getting Started page and replace every instance of wx.PySimpleApp with wx.App? Then I can also add some of what you said on your email.

Daniel.

Daniel Carrera wrote:

May I edit the Getting Started page and replace every instance of wx.PySimpleApp with wx.App?

I think that's a good idea, though make sure to make it:

wx.App(False)

What that does is keep wx from re-directing stdout and stderr to a window of its own making. Once your app is up and running, the re-direction can be useful, but if it can't even start, you get errors printed to a wx.Frame, then, when the app crashes, it immediately goes away -- very frustrating and confusing to newbies!

As Robin mentioned, the default will change with wxPython 2.9, but it won't hurt to put a False in there.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Just for completeness, I thought I should reply to the second
question. Some of my older applications use OnInit, but most of my new
stuff and the examples on my blog do not.

From what I've read in Robin's answers, it doesn't look like it
matters too much if you use OnInit or not for simple programs. I have
found OnExit to be handy though if you need to do some cleanup before
the application closes.

···

On Jan 14, 2:00 pm, Daniel Carrera <dcarr...@gmail.com> wrote:

Mike Driscoll wrote:
>> What's the difference between these? Why are there two?

>> Daniel.

> It's probably just inherited from the C++ stuff, but you'll note that
> OnInit() only applies to wx.App classes. Here's what the wxPython in
> Action book says:

Thanks. It sounds like the OnInit() method does important things. Should
that be the standard way to write wxPython apps? I notice that the
Getting Started page doesn't use this method.

What do you use in your own programs? wx.Frame with __init__ or wx.App
with OnInit() ? What is wx.App anyways?

Thanks for the help.

Daniel.

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

PyCon 2010 Atlanta Feb 19-21 http://us.pycon.org/

Christopher Barker wrote:

Daniel Carrera wrote:

May I edit the Getting Started page and replace every instance of wx.PySimpleApp with wx.App?

I think that's a good idea, though make sure to make it:

wx.App(False)

What that does is keep wx from re-directing stdout and stderr to a window of its own making. Once your app is up and running, the re-direction can be useful, but if it can't even start, you get errors printed to a wx.Frame, then, when the app crashes, it immediately goes away -- very frustrating and confusing to newbies!

Ok. I have replaced 13 instances of wx.PySimpleApp() with wx.App(False). On the first example I also explain the reason for the "False". I also added some of Robin's explanation about wx.App() and rephrased some of the explanation.

Cheers,
Daniel.