必须首先创建 wx.app 对象

发布于 2024-11-14 06:47:22 字数 732 浏览 2 评论 0原文

我的代码非常简单,但我不断收到以下错误。我研究了这个错误,它几乎表明 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情愿 2024-11-21 06:47:22

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

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

del app

我希望它能帮助你。

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.

静谧幽蓝 2024-11-21 06:47:22

您的 __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()

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()
弥繁 2024-11-21 06:47:22

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

关键是使用运行您正在运行的 Python 代码的编辑器/IDE
在外部进程中编辑,而不是在同一进程中运行
作为编辑器本身进行处理。

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.

不再让梦枯萎 2024-11-21 06:47:22

尝试关闭终端/控制台并重新运行它(如果该选项可用)。
当我在 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.

梦回旧景 2024-11-21 06:47:22

在 Psychopy 构建器中运行它,将以下内容添加到实验的开头会有所帮助:

import wx 
tmpApp = wx.PySimpleApp()

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

import wx 
tmpApp = wx.PySimpleApp()
青朷 2024-11-21 06:47:22

我遇到了同样的问题,但是:

del app

关闭窗口并停止内核,所以这没有多大帮助

我发现这对我有用:

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

没有 app=[]-part,程序运行一次,但不是第二次当它停止并给出“必须首先创建 wx.app 对象”错误的时间

希望这对其他人有用。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文