如何从 pyqtgraph TextItem 获取属性?
How do I get attributes from a pyqtgraph TextItem?
我正在使用 pyqtgraph
并尝试恢复添加到给定图形的 TextItem
class 对象的属性。
虽然看起来这是一个简单的任务,但我不知道如何提取它,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'
如果您检查 source code,您会看到 TextItem 有一个存储信息的 QGraphicsTextItem,因此如果您想获取文本信息,您应该使用该对象:
text = c.textItem.toPlainText()
print(text)
我正在使用 pyqtgraph
并尝试恢复添加到给定图形的 TextItem
class 对象的属性。
虽然看起来这是一个简单的任务,但我不知道如何提取它,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'
如果您检查 source code,您会看到 TextItem 有一个存储信息的 QGraphicsTextItem,因此如果您想获取文本信息,您应该使用该对象:
text = c.textItem.toPlainText()
print(text)