如何从pyqtgraph TextItem获取属性?

时间:2019-05-11 01:14:13

标签: python pyqtgraph

我正在使用<TextView android:id="@+id/lblDlgLogoutMessage" style="@style/Details" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="32dp" android:layout_marginEnd="16dp" android:layout_marginBottom="32dp" android:fitsSystemWindows="true" android:gravity="center_horizontal" android:text="@string/logout_msg" android:textColor="@color/black" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/lblDlgLogoutTitle" /> ,并尝试恢复添加到给定图中的pyqtgraph类对象的属性。

尽管看起来这是一个简单的任务,但我不知道如何提取它,而documentation并没有太大帮助。

这是一个片段:

TextItem

我猜对了.x()和.y()并使其正确,但是了解如何提取其他特征也很重要!在当前形式下,它会引发:

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_())

1 个答案:

答案 0 :(得分:0)

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

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