无法将Qtextedit中的文本添加到matplotlib“stickynote”

2024-06-16 10:53:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为matplotlib绘图中的注释添加一些文本,我想从QTextEdit中获取该文本,该文本位于工具栏中带有按钮的QDialog中。你知道吗

这是我目前的代码:

class TextoMatplotlib(QDialog):
    def __init__(self):
      QDialog.__init__(self)
      uic.loadUi("insertartextoenMatplotlib.ui", self) # Created in QtDesigner
      #Matplotlib figure
      fig = plt.figure(1, figsize=(5,5))
      fig.clf()
      ax = fig.add_subplot(111)
      ax.plot()
      self.connect(self.textEdit, SIGNAL("returnPressed()"),self.InsertText) 

下面是我想与QTextEdit连接的方法(它们都在同一个类中):

     def InsertText(self):
       nota.insertPlainText(self.textEdit.text())
       nota.setText('')

       notes = ax.annotate(nota, xy=(), bbox = dict(facecolor="red"))
       listadenotas = []
       for note in notes:
          nota = DraggableNote(note)
          nota.connect()
          listadenotas.append(nota)

class DraggableNote:
    def __init__(self, note):
      self.note = nota
      self.press = None

    def connect(self):
      self.cidpress = self.note.figure.canvas.mpl_connect(
          "button_pressed_event", self.on_press)
      self.cidrelease = self.note.figure.canvas.mpl_connect(
          "button_release_event", self.on_release)
      self.cidmotion = self.note.figure.canvas.mpl_connect(
          "motion_notify_event", self.on_motion)

    def on_press(self, event):
      if event.inaxes != self.note.axes:
          return
      contains, attrd = self.note.contains(event)

      if not contains:
          return
          x0, y0 = self.note.xy
          self.press = x0, y0, event.xdata, event.ydata

    def on_motion(self, event):

      if self.press is None:
          return
      if event.inaxes != self.note.axes:
          return
      x0, y0, xpress, ypress = self.press
      dx = event.xdata - xpress
      dy = event.ydata - ypress

      self.note.set_x(x0+dx)
      self.note.set_y(y0+dy)

      self.note.figure.canvas.draw()

  def on_release(self, event):

      self.press = None
      self.note.figure.canvas.draw()

  def disconnect(self):
      self.note.figure.canvas.mpl_disconnect(self.cidpress)
      self.note.figure.canvas.mpl_disconnect(self.cidrelease)
      self.note.figure.canvas.mpl_disconnect(self.cidmotion)

我从Matplotlib文档中的一个示例中得到了“DraggableNote”类。你知道吗

我不知道问题出在哪里。我可能把它复制/粘贴错了,但缩进是正确的。抱歉,如果有这样的错误。你知道吗

我希望你能帮助我。谢谢你的回答。你知道吗

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

我试着让它更容易些。我就是这么做的:

class TextoMatplotlib(QDialog):
    def __init__(self):
      QDialog.__init__(self)
      uic.loadUi("insertartextoenMatplotlib2.ui", self) 

      self.setWindowTitle("Insertar Nota")
      self.setMinimumSize(250, 120)
      self.setMaximumSize(250, 120)
      self.label.setText("Insertar texto: ")

      x = np.arange(0, 5, 0.1);
      y = np.sin(x)

      fig, ax = plt.subplots()
      ax.plot(x,y)

      nota = self.textEdit.toPlainText()
      self.connect(self.textEdit, SIGNAL("returnPressed()"), self.insertText)

    def insertText(self):
      note = ax.annotate(nota, xy=(2, 0), bbox=dict(facecolor='yellow'))  
      note.draggable()

它仍然不起作用。我真的不知道我错过了什么


Tags: selfeventinitondefconnectfigax