【问题标题】:Disconnecting a PyQt Signal in a conditional在条件中断开 PyQt 信号
【发布时间】:2016-06-22 09:22:02
【问题描述】:

我在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

【问题讨论】:

    标签: python python-2.7 matplotlib pyqt


    【解决方案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)
    

    【讨论】:

    • 我试过这样做,但它总是给我错误:A has no attribute self.`
    • 你能贴出你正在运行的代码和stacktrace吗?
    猜你喜欢
    • 2013-07-14
    • 2014-03-10
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 2011-08-08
    • 1970-01-01
    • 2016-01-28
    相关资源
    最近更新 更多