jerry jerry - 1 month ago 5
Python Question

wx.app object must be created first

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!



Answer

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