【问题标题】:How to call wx.Frame from a class when I am not using the super function?当我不使用超级函数时,如何从类中调用 wx.Frame?
【发布时间】:2013-05-28 14:54:44
【问题描述】:
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().routine 执行 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()

而是显示错误: 类型错误:找不到所需的参数“父”{pos 1}

【问题讨论】:

    标签: python-2.7 wxpython


    【解决方案1】:

    代码

    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

    【讨论】:

    • 不使用 super 命令也可以做到吗?使用 super 我更早地得到了结果。我在这里寻找的是直接使用 wx.Frame() 方法的替代方法
    • 见底部链接。
    • 问题又回到了 0 级。正如我最初的问题所说,我不想使用超级功能。直接使用 wx.Frame() 方法有什么替代方法吗?如果是,您能否将解决方案代码发布到我的问题?
    • 不,没有使用wx.Frame() 方法的解决方案。您必须改用 wx.Frame 类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多