必须首先创建 wx.app 对象
我的代码非常简单,但我不断收到以下错误。我研究了这个错误,它几乎表明 IDLE 和我自己的 GUI 正在互相搞砸,但我真的不知道如何避免它。我的意思是,如果我只是单击 GUI 的 .py 文件而不打开 IDLE,我会收到相同的错误。
有什么想法吗?
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()
错误:
PyNoAppError:必须首先创建 wx.App 对象!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我猜你在第二次调试程序时遇到了这个问题。
您可以在代码末尾添加该行。
我希望它能帮助你。
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.
I hope it can help you.
您的
__init__
函数缩进不正确。应该是Your
__init__
function is not indented properly. It should be引用自: http://wxpython-users.1045709.n5.nabble.com/PyNoAppError-The-wx-App-object-must-be-created-first-td2362821.html
Quoted from: http://wxpython-users.1045709.n5.nabble.com/PyNoAppError-The-wx-App-object-must-be-created-first-td2362821.html
尝试关闭终端/控制台并重新运行它(如果该选项可用)。
当我在 Spyder (3.3.2) 中运行与上面类似的代码时得到这个按摩时,对我有用。
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.
在 Psychopy 构建器中运行它,将以下内容添加到实验的开头会有所帮助:
Running this inside Psychopy builder, adding the following to the beginning of the experiment helped:
我遇到了同样的问题,但是:
关闭窗口并停止内核,所以这没有多大帮助
我发现这对我有用:
没有 app=[]-part,程序运行一次,但不是第二次当它停止并给出“必须首先创建 wx.app 对象”错误的时间
希望这对其他人有用。
每
I had the same problem, but the:
close the window and stop the kernel, so it was not of great help
I found that this worked for me:
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