wx.app object must be created first

13,942

Solution 1

Your __init__ function is not indented properly. It should be

 def __init__(self, parent, id):
    wx.Frame.__init__(self, parent, id, 'Title', size=(300,200))
    panel = wx.Panel(self)


    box = wx.TextEntryDialog(None, "How old are you?", "Title", "default text")
    if box.ShowModal() == wx.ID_OK:
        answer = box.GetValue()

Solution 2

I guess you encountered this problem when you were debugging your program second time.

You can add the line at the end of the code.

del app

I hope it can help you.

Solution 3

Quoted from: http://wxpython-users.1045709.n5.nabble.com/PyNoAppError-The-wx-App-object-must-be-created-first-td2362821.html

The key is to use an editor/IDE that runs the Python code you are editing in an external process, rather than running it in the same process as the editor itself.

Share:
13,942

Related videos on Youtube

jerry
Author by

jerry

please delete me

Updated on June 04, 2022

Comments

  • jerry
    jerry almost 2 years

    My code is pretty straight forward, but I keep getting the error below. I researched the error and it pretty much says IDLE and my own GUI are screwing each other up, but I don't really know how to avoid it. I mean, if I just click on the .py file for my GUI without having IDLE open, I get the same error.

    Any ideas?

    Python 2.7 Windows XP

    import wx
    
    class applicationName(wx.Frame):
    
        def __init__(self, parent, id):
            wx.Frame.__init__(self, parent, id, 'Title', size=(300,200))
            panel = wx.Panel(self)
    
    
        box = wx.TextEntryDialog(None, "How old are you?", "Title", "default text")
        if box.ShowModal() == wx.ID_OK:
            answer = box.GetValue()
    
    
    
    
    if __name__ =='__main__':
        app = wx.PySimpleApp()
        frame = applicationName(parent=None, id=-1)
        frame.Show()
        app.MainLoop()
    

    Error:

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


  • MichaelRSF
    MichaelRSF over 6 years
    That's really not a valid answer. I ran similar code. Worked first time, and now I get the same error of wx.App object must be created first
  • Sjoerd C. de Vries
    Sjoerd C. de Vries over 6 years
    Indeed, running the wxWidgets helloworld2.py example on wxpython.org/pages/overview in Spyder gave me this error. Setting the file configuration on "Execute in a dedicated console' made it work.
  • Marc
    Marc about 6 years
    Thank you, I got this same problem (only while running my code the second time) and you solved it :-)
  • Y. Chang
    Y. Chang about 6 years
    I think this is the correct answer as the checked answer didn't help but this one did.