【问题标题】:How to align the center of a textwidget in pyqtgraph如何在pyqtgraph中对齐文本小部件的中心
【发布时间】:2017-09-01 06:30:10
【问题描述】:

我想使用 textwidget 在图像上显示文本,并确保文本在图像中居中。 (我实际上想制作一部电影,其中文本随每一帧而变化,以表明发生了什么变化)。

我尝试了以下方法(在 jupyter 笔记本中):

%pylab inline
%gui qt
import pyqtgraph as pg

# new cell
imv = pg.ImageView()
imv.show()

#new cell
# add a textwidget
tw_center = pg.TextItem('')
tw_center.setFont(pg.Qt.QtGui.QFont("arial", 20))
imv.addItem(tw_center)
# set it in the center
tw_center.setPos(50, 0)
# display text in red to make it visible
tw_center.setText('hi there', (255,0,0))

# create and show data
data = np.random.rand(100,100)
imv.setImage(data)

但是,这会导致文本标签不居中,因为左角当前位于中心。如何在图像中心获得文本的中心?我的数据大小以及文本小部件中的文本可能会发生变化,因此我正在寻找一种比试错更聪明的方法。我查看了 setAnchor 方法,但它只指定角。

问候, 德克

【问题讨论】:

    标签: python pyqt4 pyqtgraph scientific-software


    【解决方案1】:

    看来您需要正确设置锚点。如果您查看documentation,您会发现您可以使用不同的锚点启动TextItem。要使其居中,您可能需要类似

    tw_center = pg.TextItem('',anchor=(0.5,0))
    

    这会将项目的锚点设置为在 x 方向居中。

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2019-03-10
      • 2021-01-03
      • 2018-11-08
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多