PyApp does not create wxApp instance

132 views
Skip to first unread message

Bazza Hill

unread,
Jan 29, 2018, 3:20:56 AM1/29/18
to wxPython-users
I have built wxPython 4 for Python3 using the source tarball on OpenSuse Leap

It builds and installs

From the python3 console (from Eric IDE)  if I type

import wx
app = wx.PyApp
frame=wx.Frame(None,wx.ID_ANY,"Test")

I get after the 3rd line:

Traceback (innermost last):
  File "<stdin>", line 1, in <module>
wx._core.PyNoAppError: The wx.App object must be created first!


What am I doing wrong?

If I use wxPython3 it works.

Regards

Bazza

Adrian Hill

unread,
Jan 29, 2018, 12:09:31 PM1/29/18
to wxPython-users
I think the second line should be

app = wx.PyApp()

(parentheses missing in the original).

Adrian

Bazza Hill

unread,
Jan 29, 2018, 1:13:26 PM1/29/18
to wxPython-users
Thanks,

However the brackets make no difference. The code (such as it is) runs using wxPython 3 (PyApp changed to App). I can show the problem is related to the way the wxApp is created. By creating the wxApp instance before invoking the python interpreter "it works".

I am minded to give up until wxPython 4 is stable.

Bazza

Steve Barnes

unread,
Jan 29, 2018, 1:52:52 PM1/29/18
to wxpytho...@googlegroups.com


On 29/01/2018 18:13, Bazza Hill wrote:
> Thanks,
>
> However the brackets make no difference. The code (such as it is) runs
> using wxPython 3 (PyApp changed to App). I can show the problem is
> related to the way the wxApp is created. By creating the wxApp instance
> before invoking the python interpreter "it works".
>
> I am minded to give up until wxPython 4 is stable.
>
> Bazza
>
Testing this on Window 10:
> ipython
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32
bit (Intel)]
Type "copyright", "credits" or "license" for more information.

IPython 5.5.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

In [1]: import wx

In [2]: pa = wx.PyApp

In [3]: pa.IsDisplayAvailable()
Out[3]: True

In [4]: frame=wx.Frame(None,wx.ID_ANY,"Test")
---------------------------------------------------------------------------
PyNoAppError Traceback (most recent call last)
<ipython-input-4-bff7e941f974> in <module>()
----> 1 frame=wx.Frame(None,wx.ID_ANY,"Test")

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

In [5]: app = wx.App()

In [6]: frame=wx.Frame(None,wx.ID_ANY,"Test")

In [7]: del frame

In [8]: exit

Steve-Gadget@MOON C:\Users\Steve-Gadget
> ipython
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32
bit (Intel)]
Type "copyright", "credits" or "license" for more information.

IPython 5.5.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

In [1]: import wx

In [2]: pa = wx.PyApp()

In [3]: frame=wx.Frame(None,wx.ID_ANY,"Test")

In [4]: exit

Steve-Gadget@MOON C:\Users\Steve-Gadget
> c:\Python36_64\Scripts\ipython.exe
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit
(AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import wx

In [2]: pa = wx.PyApp()

In [3]: frame=wx.Frame(None,wx.ID_ANY,"Test")

In [4]: exit
Steve-Gadget@MOON C:\Users\Steve-Gadget
> c:\Python36_64\python -c "import wx;print(wx.version());"
4.0.0b2 msw (phoenix)

Steve-Gadget@MOON C:\Users\Steve-Gadget
> python -c "import wx;print(wx.version());"
4.0.0b1 msw (phoenix)

--
Steve (Gadget) Barnes
Any opinions in this message are my personal opinions and do not reflect
those of my employer.

Tim Roberts

unread,
Jan 29, 2018, 2:18:36 PM1/29/18
to wxpytho...@googlegroups.com
Bazza Hill wrote:
>
> However the brackets make no difference. The code (such as it is) runs
> using wxPython 3 (PyApp changed to App). I can show the problem is
> related to the way the wxApp is created. By creating the wxApp
> instance before invoking the python interpreter "it works".

What does that mean?  Are you blending wxPython and wxWidgets C++ code
here?  Are you saying you can create a wxApp in C++ code before
launching Python and it works, but if you create a wx.App in the Python
code, it does not?

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

Bazza Hill

unread,
Jan 30, 2018, 2:29:09 AM1/30/18
to wxPython-users
Tim,
Thanks.

I am starting with wxPython 4 built from sources.

If I try this at the python 3 console:
import wx
app = wx.PyApp()
frame = wx.Frame(None, wx.ID_ANY, "Hello World")
frame.Show(True)
app.MainLoop()

It fails at "frame = wx.Frame(None, wx.ID_ANY, "Hello World")" because there is no wxApp instance.

If I write a simple wxWidgets application that invokes the python interpreter after creating a wxApp instance and run:

import wx
frame = wx.Frame(None, wx.ID_ANY, "Hello World")
frame.Show(True)

The frame shows.

Bazza

Robin Dunn

unread,
Jan 30, 2018, 5:12:18 PM1/30/18
to wxPython-users
wx.PyApp is an implementation detail and should not be used directly. Instead you should always use wx.App. All of the bootstrapping code needed to use wxWidgets from Python happens in wx.App.

Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
>>> wx.version()
'4.0.0 osx-cocoa (phoenix)'
>>> app = wx.App()
>>> frm = wx.Frame(None, title='Hello')
>>> frm.Show()
True
>>> app.MainLoop()
0

-- 
Robin Dunn
Software Craftsman

Bazza Hill

unread,
Jan 30, 2018, 11:49:31 PM1/30/18
to wxPython-users
Thanks.

That makes sense. Elsewhere it said you should use PyApp and not App.

There are other problems i_wxPyCheckForApp should check for wxAppConsole::GetInstance() being non-null, not wxTheApp. This is because the python interpreter might embedded in an application with its own wxApp.

Really the wxApp interface should be in a separate module optionally imported. I have done this and it works well both embedded and non-embedded python.

Bazza

Robin Dunn

unread,
Jan 31, 2018, 1:22:09 PM1/31/18
to wxPython-users
On Tuesday, January 30, 2018 at 8:49:31 PM UTC-8, Bazza Hill wrote:
Thanks.

That makes sense. Elsewhere it said you should use PyApp and not App.


Where did you see that?

 
There are other problems i_wxPyCheckForApp should check for wxAppConsole::GetInstance() being non-null, not wxTheApp. This is because the python interpreter might embedded in an application with its own wxApp.


Bazza Hill

unread,
Jan 31, 2018, 1:48:55 PM1/31/18
to wxPython-users
Robin,

I found the recommendation to use wx.PyApp and not wx.App in a search, along the lines of wx.App does not work but wx.PyApp does.
Reply all
Reply to author
Forward
0 new messages