当我不使用超级函数时,如何从类中调用wx.Frame?

时间:2013-05-27 08:18:59

标签: python-2.7 wxpython

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()

当我使用上面的代码时,A()。例程在类A的方法中执行命令但是当我使用代码时:

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))

类似
A().routine()

而是显示错误: TypeError:必需参数' parent' {pos 1}未找到

1 个答案:

答案 0 :(得分:0)

代码

A().routine()

创建一个 new A对象,并在那个对象上调用该方法。

要为您自己的对象调用基类方法,请使用:

super(B, self).routine()

对于您的示例框架,请使用:

class Example(wx.Frame):
    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title, size=(300, 200))
        ...

如果您确实不想使用super,请明确调用基类方法:

class Example(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(300, 200))
        ...

另见Understanding Python super() with __init__() methods