在我对以下程序进行一些更改之前,一切都很顺利:
#! /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的新手,请帮忙。 THX。
app = MyApp()
标准输出/标准错误:
NameError: global name 'Show' is not defined
答案 0 :(得分: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()