【问题标题】:Python, wxPython errorPython,wxPython 错误
【发布时间】:2012-12-17 03:37:34
【问题描述】:

谁能帮我找出我做错了什么我对 GUI 的经验很少。 代码:

import wx
    class bucky(wx.Frame):
            #constructor
        def __init__(self,parent,id):
            wx.Frame.__init__(self,parent,id,'Frame aka window',size=(300,200))
            panel=wx.Panel(self)
            button=wx.Button(panel,label="exit",pos=(130,10),size=(60,60))
            self.Bind(wx.EVT_BUTTON, self.closebutton,button)
            self.Bind(wx.EVT_CLOSE, self.closewindow)
        def closebutton(self,event):
            self.close(True)
        def closewindow(self,event):
            self.Destroy()
    if __name__=='__main__':
        app=wx.PySimpleApp()
        frame=bucky(parent=None,id=-1)
        frame.Show()
        app.MainLoop()

错误:

PyNoAppError: The wx.App object must be created first!

win32ui.error: Error in Command Message handler for command ID 36864, Code 0

【问题讨论】:

  • 请尝试修复代码的缩进,以便所有内容都按照在文本编辑器中显示的方式缩进 - 这在 Python 中非常重要。
  • 我想你不想要这里的第三个参数self.Bind(wx.EVT_BUTTON, self.closebutton,button)
  • 这是完整的回溯吗?它是否告诉您该错误来自哪一行?
  • @JoranBeasley - 实际上这是正确的。请参阅wiki.wxpython.org/self.Bind%20vs.%20self.button.Bind 了解更多信息
  • 哦,对了,我以为他是直接绑定到按钮上的:P

标签: python wxpython pywin


【解决方案1】:

此代码在带有 wxPython 2.8.12.1 和 Python 2.6.6 的 Windows 7 上为我运行。您使用的是什么操作系统和 Python 版本?当我在 IDLE 中运行我的代码时,我不时看到此错误消息。如果你正在这样做,那就不要这样做。 Tkinter 的主循环(这是 IDLE 的组成部分)会干扰其他 GUI 工具包的主循环​​。

closebutton 方法存在一个问题,它调用了一个不存在的“关闭”方法。

【讨论】:

    猜你喜欢
    • 2015-08-03
    • 2019-03-21
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多