Python 2继承自调用类和另一个指定的类

时间:2018-01-08 16:47:49

标签: python-2.7 class matplotlib super

这是我的设置。这不是所有代码,只是重要部分

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar

class MyToolbar(NavigationToolbar):
def __init__(self, figure_canvas, parent, main):
    self.toolitems = (
    ('Home', 'Reset Original View', 'home', 'home'),    # (Title, Tooltip, Icon, callback function)
    ('Back', 'Back to previous view', 'back', 'back'),
    ('Forward', 'Forward to next view', 'forward', 'forward'),
    ('Clear', 'Clear all graphs', 'clearIcon', 'on_click_clear_graph'),
    )

    NavigationToolbar.__init__(self, figure_canvas, parent)

    def on_click_clear_graph(self):
        # I need to access calling class' (PlotWindow's) clearAx method here
        super(MyToolbar,self).clearAx() # but this doesn't work

class PlotWindow():
    def __init__(self):
        self.figure = Figure()
        self.ax = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self.figure)
        self.toolbar = MyToolbar(self.canvas, self, parent)
    def clearAX(self):
        self.ax.clear()

我使用MyToolbar类为Matplotlib工具栏添加额外的工具,但我还需要访问PlotWindow类中的变量。

我一直在程序的其他部分使用super(),我可以使用它来访问父类,但在这种情况下我似乎无法让它工作。

我错过了部分内容吗?

如果以上代码没有意义,也许可以使用下面的例子。

class A(SomeOtherInheritedClass):
    # how can I call print_something from class B here 
    # without interfering with SomeOtherInheritedClass
class B():
    a = A()
    def print_something(self):
        print "something"

0 个答案:

没有答案