必须先创建wx.app对象

jer*_*rry 4 python user-interface wxpython

我的代码很简单,但我不断收到以下错误.我研究了错误,它几乎说IDLE和我自己的GUI相互搞砸,但我真的不知道如何避免它.我的意思是,如果我只是在没有IDLE打开的情况下点击我的GUI的.py文件,我会得到同样的错误.

有任何想法吗?

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()
Run Code Online (Sandbox Code Playgroud)

错误:

PyNoAppError:必须首先创建wx.App对象!


小智 8

我猜你第二次调试你的程序时遇到了这个问题.

您可以在代码末尾添加该行.

del app
Run Code Online (Sandbox Code Playgroud)

我希望它可以帮到你.


Ste*_*rry 5

您的__init__函数缩进不正确。它应该是

 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()
Run Code Online (Sandbox Code Playgroud)