如何从pyqtgraph的TextItem获取属性?

2024-10-01 09:31:25 发布

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

我正在使用pyqtgraph并尝试恢复添加到给定图形中的TextItem类对象的属性。你知道吗

虽然看起来这是一个简单的任务,但我不知道如何提取它,而且documentation也没有多大帮助。你知道吗

下面是一个片段:

import sys
from PyQt5.QtWidgets import QApplication, QWidget
import pyqtgraph as pg
import numpy as np

def refreshScreen(AnnotationsList):
        for i in range(len(AnnotationsList)):
              c = AnnotationsList[i]

              # Now I need to extract information from the annotations:
              x = c.x()
              print(x)

              y = c.y()
              print(y)

              text = c.text()
              print(text)

              OtherProperties = c.getProperty()
              print(OtherProperties)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = QWidget()
    w.resize(250, 150)
    w.move(300, 300)
    w.setWindowTitle('Simple')
    w.show()

    AnnotationsList = []
    c = pg.TextItem(anchor=(0,0), border=pg.mkPen(200, 200, 200))
    c.setText(text='my_annotation', color=(0,0,0))
    # coordinates for annotation
    x = 5
    y = 10
    c.setPos(x,y)
    AnnotationsList = np.append(AnnotationsList, c)

    refreshScreen(AnnotationsList)

    sys.exit(app.exec_())

我猜到了.x()和.y(),并且把它们猜对了,但是知道如何提取其他特性也很重要!在当前的形式中,它引发了:

AttributeError: 'TextItem' object has no attribute 'text'

Tags: textfromimportforasnpsyspyqtgraph
1条回答
网友
1楼 · 发布于 2024-10-01 09:31:25

如果选中source code,您会看到TextItem有一个QGraphicsTextItem来存储信息,因此如果要获取文本信息,应该使用该对象:

text = c.textItem.toPlainText()
print(text)

相关问题 更多 >