Python 2.7 当我不使用超级函数时,如何从类调用wx.Frame?
当我使用上述代码时,A()例程执行类A方法中的命令,但当我使用代码时:Python 2.7 当我不使用超级函数时,如何从类调用wx.Frame?,python-2.7,wxpython,Python 2.7,Wxpython,当我使用上述代码时,A()例程执行类A方法中的命令,但当我使用代码时: class A(object): def routine(self): print "A.routine()" class B(A): def routine(self): print "B.routine()" A().routine() def fun(): b = B() b.routine() if __name__ == '__mai
class A(object):
def routine(self):
print "A.routine()"
class B(A):
def routine(self):
print "B.routine()"
A().routine()
def fun():
b = B()
b.routine()
if __name__ == '__main__':fun()
为什么会这样
import wx
class Example(wx.Frame):
def __init__(self, parent, title):
wx.Frame().__init__(parent, title=title, size=(300, 200))
self.Centre()
self.Show()
if __name__ == '__main__':
app = wx.App()
Example(None, title='Size')
app.MainLoop()
工作原理与
wx.Frame().__init__(parent, title=title, size=(300, 200))
而是显示错误:
TypeError:找不到必需的参数“parent”{pos 1}代码
A().routine()
创建一个新的对象并调用该对象上的方法
要为您自己的对象调用基类方法,请使用以下命令:
A().routine()
对于示例框架,请使用:
super(B, self).routine()
如果确实不想使用super
,请显式调用基类方法:
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title, size=(300, 200))
...
另请参见。不使用super命令也可以这样做吗?使用super,我甚至更早得到结果。我在这里寻找的是一种直接使用wx.Frame()方法的替代方法。请参见底部的链接。问题再次回到0级。正如我最初的问题所说,我不想使用超级函数。直接使用wx.Frame()方法是否有其他选择?如果是,请将解决方案代码发布到我的问题上好吗?不,没有使用wx.Frame()
方法的解决方案。您必须改用wx.Frame
类。
class Example(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(300, 200))
...