我正在尝试在 matplotlib 图中为注释添加一些文本,并且我想从 a 中获取该文本,该文本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()
它仍然不起作用。我真的不知道我在这里错过了什么