从matplotlib的自定义工具栏按钮调用另一个类中的方法

时间:2016-05-29 19:11:50

标签: python matplotlib pyqt

我正在使用PyQtMatplotlib库来创建应用程序。我创建了一个我的QMainWindow的子类,我不知道如何将新类的方法连接到第一个类中的方法。

首先,我在QPushButton中创建了QMainWindow,如下所示:

class A(QMainWindow):
  def __init__(self):
    QMainWindow.__init__(self)

    self.mainWidget = QWidget()
    self.setCentralWidget(self.mainWidget)

    layout = QVBoxLayout()
    self.mainWidget.setLayout(layout)

    self.figure_canvas = FigureCanvas(Figure(facecolor="lightblue"))
    layout.addWidget(self.figure_canvas, 10)

    self.axes = self.figure_canvas.figure.add_subplot(111)

    #Here is the first button creatd and added to the navigation toolbar
    #using addWidget
    self.btn_selection_tool = QPushButton(QIcon("select.png"), "")
    self.navigation_toolbar.addWidget(self.btn_selection_tool)

    #And here is how i connected this button to two methods
    self.connect(self.btn_selection_tool, SIGNAL("clicked()"), self.method1)
    self.connect(self.btn_selection_tool, SIGNAL("clicked()"), self.method2)

但是现在,我必须创建一个新类(B类),我需要将{1}中QPushButton之前调用的方法连接到之前调用的方法。但是,在这种情况下,我没有按钮来做到这一点。我只需要通过class A中的方法在class A中“触发”此方法。

我的问题是我不能像我创建class B时那样调用class A(self.method1和self.method2)中的方法。这是在创建子类之前。

我尝试过:

QPushbutton

我该怎么做?希望你能帮帮我。

------编辑-----

这两种方法是:

def method_in_B_class(self):
  self.a  = A()
  self.a.method1()
  self.a.method2()

所以,当他们使用时调用:

def method1(self):
    self.figure_canvas.setCursor(Qt.IBeamCursor)
    self.selection_mode = None
    #self.selectionmode is a variable that I change when method1 is called
    #and I can go through the "if" in method2

def method2(self):
    #I define some conditions with another variables
    if len(self.axes.lines) >= 1 and self.selection_mode == None and A.send_msg == None:

        self.cid_press = self.figure_canvas.mpl_connect("button_press_event", self.method3)
        self.cid_release = self.figure_canvas.mpl_connect("button_release_event", self.method4)

    elif self.selection_mode == "disable":
        self.disconnect(self)

他们被实例化,但随后我们可能会“完成”。这是def method_in_B_class(self): self.a = A() self.a.method1() self.a.method2() 不会更改,QtCursormethod3不会被调用。

0 个答案:

没有答案