背景:
使用wxPython时,创建了wx.App类子类MyApp()和方法OnInit()后,运行程序主窗口出现后立即退出,并报TypeError: invalid result from MyApp.OnInit(), a 'bool' is expected not 'NoneType'
原因:
OnInit()方法没有return Ture,在后面加上这句代码即可
class MyApp(wx.App):
def __init__(self):
wx.App.__init__(self)
def OnInit(self):
self.frame = MyFrame(None)
self.frame.Show()
self.SetTopWindow(self.frame)
return True #否则报a 'bool' is expected not 'NoneType'
解释:
OnInit()
方法在主程序开始前被wxPython
系统调用。这个方法不要求参数并返回一个布尔值,如果所返回的值是False
,则应用程序将立即退出。
扩展
OnInit()
方法是wxPython
架构的一部分,所以任何关于你的定制的类的所需的初始化通常都由OnInit()
方法管理,而不在Python
的__init__
方法中。