Newbie Help.

I am trying to create a simple GUI and I have the following
code.

…Separate file for my little class …App.py

import wx

class MyApp(wx.App):

def **__init__**(*self*):

    frame = wx.Frame(None, -1, *'Welcome to Elmer'*, size=(400, 200))         

    frame.Show(True)

    SetTopWindow(frame)

return True

image001.gif

···

…In my main file…

import
wx # use the wxPython module

import App

an_app = App.MyApp()

an_app.MainLoop()

When I run this I get the following error.

B:\CodeWorkSpace\Python\CAD_GUI_TOOL\src>test.py

Traceback (most recent call last):

File “B:\CodeWorkSpace\Python\CAD_GUI_TOOL\src\test.py”,
line 39, in

an_app = App.MyApp()

File “B:\CodeWorkSpace\Python\CAD_GUI_TOOL\src\App.py”,
line 20, in init

frame = wx.Frame(None, -1, 'Welcome to

Elmer’, size=(400, 200))

File
“C:\Python26\lib\site-packages\wx-2.8-msw-ansi\wx_windows.py”, line
505,

in init

windows.Frame_swiginit(self,windows.new_Frame(*args, **kwargs))

wx._core.PyNoAppError: The wx.App object must be created
first!

B:\CodeWorkSpace\Python\CAD_GUI_TOOL\src>

Can anyone help? I am confused why this does not work.
By inheriting the wx.App class isn’t my MyApp object also a wx.App object?
Isn’t the wx.App constructor called at some point when my MyApp object is
created?

Thanks,

CH

Christopher Hogan

Hardware Engineer II

D

509.343.3522

M

509.993.2120

P

206.272.5555

F

509.343.3501

****www.f5.com


Posting again, first time did not seem to work...

I am trying to create a simple GUI and I have the following code.

.Separate file for my little class .App.py
import wx

class MyApp(wx.App):
def __init__(self):
frame = wx.Frame(None, -1, 'Welcome to Elmer', size=(400, 200))
frame.Show(True)
SetTopWindow(frame)
return True

···

---------------------------------------------------------------------------------------------------------
.In my main file.

import wx # use the wxPython module
import App

an_app = App.MyApp()
an_app.MainLoop()

When I run this I get the following error.

B:\CodeWorkSpace\Python\CAD_GUI_TOOL\src>test.py
Traceback (most recent call last):
File "B:\CodeWorkSpace\Python\CAD_GUI_TOOL\src\test.py", line 39, in <module>

an\_app = App\.MyApp\(\)

File "B:\CodeWorkSpace\Python\CAD_GUI_TOOL\src\App.py", line 20, in __init__
frame = wx.Frame(None, -1, 'Welcome to Elmer', size=(400, 200))
File "C:\Python26\lib\site-packages\wx-2.8-msw-ansi\wx\_windows.py", line 505,
in __init__
_windows_.Frame_swiginit(self,_windows_.new_Frame(*args, **kwargs))
wx._core.PyNoAppError: The wx.App object must be created first!

B:\CodeWorkSpace\Python\CAD_GUI_TOOL\src>

Can anyone help? I am confused why this does not work. By inheriting the wx.App class isn't my MyApp object also a wx.App object? Isn't the wx.App constructor called at some point when my MyApp object is created?

Thanks,
CH

Chris Hogan wrote:

I am trying to create a simple GUI and I have the following code.

class *MyApp*(wx.App):

    def *__init__*(/self/):

        frame = wx.Frame(None, -1, /'Welcome to Elmer'/, size=(400, 200))

        frame.Show(True)

        SetTopWindow(frame)

        return True

You either need to call the base class's __init__ or rename this method to OnInit.

···

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

Chris Hogan wrote:

I am trying to create a simple GUI and I have the following code.

…Separate file for my little class …App.py

import wx

class *MyApp*(wx.App):

    def *__init__*(/self/):

        frame = wx.Frame(None, -1, /'Welcome to Elmer'/, size=(400,
200))

        frame.Show(True)

        SetTopWindow(frame)

        return True

---------------------------------------------------------------------------------------------------------

…In my main file…

import wx # use the wxPython module

import App

an_app = App.MyApp()

an_app.MainLoop()

...

Can anyone help? I am confused why this does not work. By inheriting
the wx.App class isn’t my MyApp object also a wx.App object? Isn’t
the wx.App constructor called at some point when my MyApp object is
created?

No, only if YOU call it, and here you do not. Unlike C++, Python does
not automatically call a base class constructor from a derived class
constructor. You would have to call wx.App.__init__ specifically.
However, even that is not the right thing to do here.

The right thing to do is to change your App.py class so that the
initialization function is called OnInit instead of __init__. That way,
the stock wx.App constructor can do its thing. OnInit is a well-defined
callback that will be called after the application object has been
constructed enough to support window creation.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Hello,

I am trying to create a simple GUI and I have the following code.

…Separate file for my little class …App.py

import wx

class MyApp(wx.App):

def \_\_init\_\_\(self\):

    frame = wx\.Frame\(None, \-1, &#39;Welcome to Elmer&#39;, size=\(400,

200))

    frame\.Show\(True\)

    SetTopWindow\(frame\)

    return True

<snip>

Can anyone help? I am confused why this does not work. By inheriting the
wx.App class isn’t my MyApp object also a wx.App object? Isn’t the wx.App
constructor called at some point when my MyApp object is created?

Its because you are not calling the super classes (wx.App) __init__
method in your overridden one so the app object is not being fully
instantiated.

i.e)

def __init__(self)
    wx.App.__init__(self)

    your other code...

Cody

···

On Fri, May 8, 2009 at 12:09 PM, Chris Hogan <C.Hogan@f5.com> wrote:

Hi,

I am trying to create a simple GUI and I have the following code.

…Separate file for my little class …App.py

import wx

class *MyApp*(wx.App):

def *__init__*(/self/):

frame = wx.Frame(None, -1, /'Welcome to Elmer'/, size=(400, 200))

frame.Show(True)

SetTopWindow(frame)

return True

There are two problems here. The first one is that you need to initialize the wx.App. The preferred way is to do something like this:

<code>

def __init__(self, redirect=False, filename=None):
wx.App.__init__(self, redirect, filename)

</code>

And then follow that with the rest of your code, except for the SetTopWindow which is your second problem since that is currently not defined. If I run your code as is, that will throw an exception as well.

Can anyone help? I am confused why this does not work. By inheriting the wx.App class isn’t my MyApp object also a wx.App object? Isn’t the wx.App constructor called at some point when my MyApp object is created?

Thanks,

CH

As I understand it, when you subclass wx.App AND create an __init__ function, you are saying that you will create your own constructor, which is what I showed you how to do above.The __init__ is the constructor...

FYI. It is good practice to include a panel object so that the window will look "right" on Windows too. So something like this would suffice:

myPanel = wx.Panel(myFrame, wx.ID_ANY)

HTH

···

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

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4063 (20090508) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Simply put, if you inherrit from a class
you must init that class from within your own. This is true of all python,
not just wxpython.

class MyApp(wx.App):

def __init__(self):

wx.App.__init__(self)

-Brian

Brian Fett

1280 Disc Dr

SHK224

Shakopee, MN 55379

Phone: (952)402-2595

Brian.D.Fett@seagate.com

Chris Hogan C.Hogan@F5.com

Sent by: wxpython-users-bounces@lists.wxwidgets.org

No Phone Info Available
05/08/2009 12:14 PM

Please respond to

wxpython-users@lists.wxwidgets.org

To

“‘wxpython-users@lists.wxwidgets.org’”
wxpython-users@lists.wxwidgets.org
cc

Subject

[wxpython-users] Newbie Help, again.

`Posting again, first time did not seem to work…

I am trying to create a simple GUI and I have the following code.

.Separate file for my little class .App.py

import wx

class MyApp(wx.App):

def __init__(self):

    frame = wx.Frame(None, -1, 'Welcome

to Elmer’, size=(400, 200))

    frame.Show(True)

    SetTopWindow(frame)

    return True
···

.In my main file.

import wx # use the
wxPython module

import App

an_app = App.MyApp()

an_app.MainLoop()

When I run this I get the following error.

B:\CodeWorkSpace\Python\CAD_GUI_TOOL\src>test.py

Traceback (most recent call last):

File “B:\CodeWorkSpace\Python\CAD_GUI_TOOL\src\test.py”,
line 39, in

an_app = App.MyApp()

File “B:\CodeWorkSpace\Python\CAD_GUI_TOOL\src\App.py”,
line 20, in init

frame = wx.Frame(None, -1, 'Welcome to Elmer', size=(400,

200))

File “C:\Python26\lib\site-packages\wx-2.8-msw-ansi\wx_windows.py”,
line 505,

in init

_windows_.Frame_swiginit(self,_windows_.new_Frame(*args,

**kwargs))

wx._core.PyNoAppError: The wx.App object must be created first!

B:\CodeWorkSpace\Python\CAD_GUI_TOOL\src>

Can anyone help? I am confused why this does not work. By inheriting
the wx.App class isn’t my MyApp object also a wx.App object? Isn’t
the wx.App constructor called at some point when my MyApp object is created?

Thanks,

CH


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

`

Tim and Robin,

Thanks this helps. I did not understand the different between "__init__" and "OnInit".

···

-----Original Message-----
From: wxpython-users-bounces@lists.wxwidgets.org [mailto:wxpython-users-bounces@lists.wxwidgets.org] On Behalf Of Tim Roberts
Sent: Friday, May 08, 2009 12:04 PM
To: wxpython-users@lists.wxwidgets.org
Subject: Re: [wxpython-users] Newbie Help.

Chris Hogan wrote:

I am trying to create a simple GUI and I have the following code.

...Separate file for my little class ...App.py

import wx

class *MyApp*(wx.App):

    def *__init__*(/self/):

        frame = wx.Frame(None, -1, /'Welcome to Elmer'/, size=(400,
200))

        frame.Show(True)

        SetTopWindow(frame)

        return True

---------------------------------------------------------------------------------------------------------

...In my main file...

import wx # use the wxPython module

import App

an_app = App.MyApp()

an_app.MainLoop()

...

Can anyone help? I am confused why this does not work. By inheriting
the wx.App class isn't my MyApp object also a wx.App object? Isn't
the wx.App constructor called at some point when my MyApp object is
created?

No, only if YOU call it, and here you do not. Unlike C++, Python does
not automatically call a base class constructor from a derived class
constructor. You would have to call wx.App.__init__ specifically.
However, even that is not the right thing to do here.

The right thing to do is to change your App.py class so that the
initialization function is called OnInit instead of __init__. That way,
the stock wx.App constructor can do its thing. OnInit is a well-defined
callback that will be called after the application object has been
constructed enough to support window creation.

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Thanks for all the help and examples.

Now I have a new question.

Is it foolish to do something like this:

  class MyApp(wx.App, wx.Frame)
    
If this is legitimate, when I override the "__init__" function do I need to call both the wx.App "__init__" and wx.Frame "__init__"?

Or, if this is generally not a good idea, what is the downside to this implementation?

Appreciate all the input,
CH

···

-----Original Message-----
From: wxpython-users-bounces@lists.wxwidgets.org [mailto:wxpython-users-bounces@lists.wxwidgets.org] On Behalf Of Mike Driscoll
Sent: Friday, May 08, 2009 12:28 PM
To: wxpython-users@lists.wxwidgets.org
Subject: Re: [wxpython-users] Newbie Help.

Hi,

I am trying to create a simple GUI and I have the following code.

...Separate file for my little class ...App.py

import wx

class *MyApp*(wx.App):

def *__init__*(/self/):

frame = wx.Frame(None, -1, /'Welcome to Elmer'/, size=(400, 200))

frame.Show(True)

SetTopWindow(frame)

return True

There are two problems here. The first one is that you need to
initialize the wx.App. The preferred way is to do something like this:

<code>

def __init__(self, redirect=False, filename=None):
wx.App.__init__(self, redirect, filename)

</code>

And then follow that with the rest of your code, except for the
SetTopWindow which is your second problem since that is currently not
defined. If I run your code as is, that will throw an exception as well.

Can anyone help? I am confused why this does not work. By inheriting
the wx.App class isn't my MyApp object also a wx.App object? Isn't the
wx.App constructor called at some point when my MyApp object is created?

Thanks,

CH

As I understand it, when you subclass wx.App AND create an __init__
function, you are saying that you will create your own constructor,
which is what I showed you how to do above.The __init__ is the
constructor...

FYI. It is good practice to include a panel object so that the window
will look "right" on Windows too. So something like this would suffice:

myPanel = wx.Panel(myFrame, wx.ID_ANY)

HTH

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

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4063 (20090508) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Or you can use OnInit instead, as Tim and Robin pointed out…

Mike

···

On Fri, May 8, 2009 at 3:15 PM, brian.d.fett@seagate.com wrote:

Simply put, if you inherrit from a class
you must init that class from within your own. This is true of all python,
not just wxpython.

class MyApp(wx.App):

def __init__(self):

wx.App.__init__(self)

-Brian

Brian Fett

1280 Disc Dr

SHK224

Shakopee, MN 55379

Phone: (952)402-2595

Brian.D.Fett@seagate.com

Chris Hogan C.Hogan@F5.com

Sent by: wxpython-users-bounces@lists.wxwidgets.org

No Phone Info Available
05/08/2009 12:14 PM

Please respond to

wxpython-users@lists.wxwidgets.org

To

“‘wxpython-users@lists.wxwidgets.org’”
wxpython-users@lists.wxwidgets.org
cc

Subject

[wxpython-users] Newbie Help, again.

`Posting again, first time did not seem to work…

I am trying to create a simple GUI and I have the following code.

.Separate file for my little class .App.py

import wx

class MyApp(wx.App):

def __init__(self):

    frame = wx.Frame(None, -1, 'Welcome

to Elmer’, size=(400, 200))

    frame.Show(True)

    SetTopWindow(frame)

    return True

.In my main file.

import wx # use the
wxPython module

import App

an_app = App.MyApp()

an_app.MainLoop()

When I run this I get the following error.

B:\CodeWorkSpace\Python\CAD_GUI_TOOL\src>test.py

Traceback (most recent call last):

File “B:\CodeWorkSpace\Python\CAD_GUI_TOOL\src\test.py”,
line 39, in

an_app = App.MyApp()

File “B:\CodeWorkSpace\Python\CAD_GUI_TOOL\src\App.py”,
line 20, in init

frame = wx.Frame(None, -1, 'Welcome to Elmer', size=(400,

200))

File “C:\Python26\lib\site-packages\wx-2.8-msw-ansi\wx_windows.py”,
line 505,

in init

_windows_.Frame_swiginit(self,_windows_.new_Frame(*args,

**kwargs))

wx._core.PyNoAppError: The wx.App object must be created first!

B:\CodeWorkSpace\Python\CAD_GUI_TOOL\src>

Can anyone help? I am confused why this does not work. By inheriting
the wx.App class isn’t my MyApp object also a wx.App object? Isn’t
the wx.App constructor called at some point when my MyApp object is created?

Thanks,

CH

`

Chris Hogan wrote:

Now I have a new question.

Is it foolish to do something like this:

  class MyApp(wx.App, wx.Frame)

probably -- you certainly can't derive from more than one wxWindow. If wxApp and wx.Frame share any attribute names, things could get ugly. AS a rule, you can't do multiple inheritance without the classes (or one of them anyway) being designed with that in mind.
      

If this is legitimate, when I override the "__init__" function do I need to call both the wx.App "__init__" and wx.Frame "__init__"?

yes.

Or, if this is generally not a good idea, what is the downside to this implementation?

It doesn't strike me as good OO design. A App and a Frame are different animals, why mix them? As an example, an App can have more than one frame. It's the difference between a "has a" and a "is a" relationship:

Your app IS A wx.App, so you derive form it.

Your app HAS a wx.Frame, so it is a frame attribute, but it doesn't derive from it.

-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

Chris Hogan wrote:

Thanks for all the help and examples.

Now I have a new question.

Is it foolish to do something like this:

  class MyApp(wx.App, wx.Frame)
    
If this is legitimate, when I override the "__init__" function do I need to call both the wx.App "__init__" and wx.Frame "__init__"?

Or, if this is generally not a good idea, what is the downside to this implementation?
  
This is a simple question with a complicated answer.

It is legal in Python for a single class to have multiple base classes,
and perhaps you already knew that. However, in the case of wx, the
answer is more complicated. First, remember that wxPython is basically
a thin wrapper around the wxWidgets C++ library, and that affects the
amount of trickery you can do. There's stuff going on behind the scenes
that Python doesn't necessarily know about. Second, many of the classes
in wx derive from common roots, and because of that, they will have many
methods in common. In your example, if wx.App and wx.Frame both have a
method called "OnInit", the wx.Frame version will block the wx.App version.

There are some wx classes that are designed as "mix-ins", meaning that
they "add value" to another class. These are not such.

Philosophically speaking -- and here's where we get into opinions -- I
would call this a bad idea, just because it violates encapsulation. An
application is not a frame. You should have a distinct application
object, which owns a frame object, which in turn owns panels, controls
and sizers. You really need to think in object-oriented terms in order
to achieve the third level of nirvana with wxPython.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Hi,

···

On Fri, May 8, 2009 at 4:08 PM, Chris Hogan C.Hogan@f5.com wrote:

Thanks for all the help and examples.

Now I have a new question.

Is it foolish to do something like this:

    class MyApp(wx.App, wx.Frame)

If this is legitimate, when I override the “init” function do I need to call both the wx.App “init” and wx.Frame “init”?

Or, if this is generally not a good idea, what is the downside to this implementation?

Appreciate all the input,

CH

As an addendum to what the others have said, I think you would benefit from reading the wxPython Style Guide as well:

http://wiki.wxpython.org/wxPython%20Style%20Guide

  • Mike