0

在我对以下程序进行一些更改之前,一切都很顺利:

修改前的程序:

#! /usr/bin/env python
""" A bare-minimum wxPython program """

import wx

class MyApp(wx.App):
    def OnInit(self):
        return True

class MyFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title)

if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame(None, "Sample")
    frame.Show(True)
    app.MainLoop()

但是在我frame输入 的定义后OnInit,程序运行时没有语法错误,但没有显示任何内容。:(

修改后的程序:

#! /usr/bin/env python
""" A bare-minimum wxPython program """

import wx

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, "Sample")    ## add two lines here
        self.frame.Show(True)
        return True

class MyFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title)

if __name__ == '__main__':
    app = wx.App() 
    app.MainLoop()

我尝试使用调试器并单步执行程序。似乎self.frame没有定义(甚至从头到尾都没有出现)。

我的程序出了什么问题?我对 Python 和 wxPython 很陌生,请帮忙。谢谢。

编辑:

app = MyApp()

标准输出/标准错误:
NameError: global name 'Show' is not defined

4

1 回答 1

1

您应该创建MyApp(不是wx.App)对象:

#! /usr/bin/env python
""" A bare-minimum wxPython program """

import wx

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, "Sample")    ## add two lines here
        self.frame.Show(True)
        return True

class MyFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title)

if __name__ == '__main__':
    app = MyApp() # <---
    app.MainLoop()
于 2013-09-19T15:21:38.977 回答