开发者

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 t开发者_如何学编程ext")
    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!



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.


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


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.


Try closing the terminal/console and re-running it (if the option is available). worked for me when i got this massage in Spyder (3.3.2) when ran similar code to the above.


Running this inside Psychopy builder, adding the following to the beginning of the experiment helped:

import wx 
tmpApp = wx.PySimpleApp()


I had the same problem, but the:

del app

close the window and stop the kernel, so it was not of great help

I found that this worked for me:

app=[]; app = wx.App(None)

whitout the app=[]-part, the program run once, but not the second time when it stops and gives the "wx.app object must be created first"-error

Hope this can be of use for others.

Per

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜