2016-03-08 34 views
0

我在PyQt中遇到了一些鼠標事件的麻煩。這是代碼:在條件中斷開PyQt信號

class A(QMainWindow): 

    var = None 

    def __init__(self): 
    QMainWindow.__init__(self) 
    #Here I draw a matplotlib figure 

    self.figure_canvas = FigureCanvas(Figure()) 
    layout.addWidget(self.figure_canvas, 10) 
    self.axes = self.figure_canvas.figure.add_subplot(211) 

    #I created a toolbar for the figure and I added a QPushButton 

    self.btn_selection_tool = QPushButton() 
    self.navigation_toolbar.addWidget(self.btn_selection_tool) 
    self.connect(self.btn_selection_tool, SIGNAL("clicked()"), self.B) 

    def B(self): 
    if self.var == 1: 
     cid = self.figure_canvas.mpl_connect("press_button_event", self.C) 

    def C(self, event): 
    x = xdata.event 
    #I draw a line every time I click in the canvas 

    def D(self): 
    #Here I tried to call this method and disconnect the signal 
    self.figure_canvas.mpl_disconnect(cid) 

的問題是,我無法通過斷開鼠標事件的信號:

self.figure_canvas.mpl_disconnect(cid)

什麼也沒有發生,我把畫線每點擊我做。鼠標事件仍然連接。

如何斷開信號?也許使用另一個QPushButton

回答

1

您是否將連接存儲在某個地方?您可能需要存儲在一個變量正常斷開:

class A(QMainWindow): 

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

     self.cid = None 

    def B(self): 
     if self.var == 1: 
      self.cid = self.figure_canvas.mpl_connect("press_button_event", self.C) 

    def D(self): 
     if self.cid is not None: 
      self.figure_canvas.mpl_disconnect(self.cid) 
+0

我試着這樣做,但它總是給我的錯誤:'A有self.'' –

+0

您可以發佈你的代碼沒有屬性運行和堆棧跟蹤? – mfessenden