Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用PyQtGraph内的ImageView固定文本位置&;PySide2_Python_Pyside2_Pyqtgraph - Fatal编程技术网

Python 使用PyQtGraph内的ImageView固定文本位置&;PySide2

Python 使用PyQtGraph内的ImageView固定文本位置&;PySide2,python,pyside2,pyqtgraph,Python,Pyside2,Pyqtgraph,我使用PyQtGraph和PySide2显示平面2D图像和3D体积的2D切片(CT/MRI体积数据集等),用户可以在其中平移/缩放、滚动等 我想做的是在视图中的几个位置有文本,覆盖在图像上,例如在角落里——我可以指定的地方。 我希望此文本保持其屏幕位置,而不考虑图像平移/缩放等。 我还希望在用户进行查看更改(例如查看参数,如像素大小)时实时更新部分文本 在我看来,最合适的选择是一个传奇项目。有问题- 如何禁用用户拖动 如何控制文本位置?.setPos()方法无效 如何删除/隐藏图例文本左侧的行

我使用PyQtGraph和PySide2显示平面2D图像和3D体积的2D切片(CT/MRI体积数据集等),用户可以在其中平移/缩放、滚动等

我想做的是在视图中的几个位置有文本,覆盖在图像上,例如在角落里——我可以指定的地方。 我希望此文本保持其屏幕位置,而不考虑图像平移/缩放等。 我还希望在用户进行查看更改(例如查看参数,如像素大小)时实时更新部分文本

在我看来,最合适的选择是一个传奇项目。有问题-

  • 如何禁用用户拖动
  • 如何控制文本位置?.setPos()方法无效
  • 如何删除/隐藏图例文本左侧的行
另一种选择是LabelItem或TextItem,尽管我找不到一种方法来分配屏幕位置,而不是图像位置。ie-我如何指定视图窗口的左下角而不是图像的左下角-因为图像当然可以移动

-是否有方法固定相对于视口的标签/文本位置

有趣的是,LabelItem随图像平移和缩放,而TextItem仅随图像平移

下面是我的最低工作代码和每个文本的例子

from PySide2.QtWidgets import QApplication
from PySide2.QtWidgets import QMainWindow
from PySide2.QtWidgets import QWidget
from PySide2.QtWidgets import QHBoxLayout

import pyqtgraph as pg
import numpy as np
import sys


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.cw = QWidget(self)
        self.cw.setAutoFillBackground(True)
        self.setCentralWidget(self.cw)

        self.layout = QHBoxLayout()
        self.cw.setLayout(self.layout)

        self.DcmImgWidget = MyImageWidget(parent=self)
        self.layout.addWidget(self.DcmImgWidget)

        self.show()


class MyImageWidget(pg.ImageView):
    def __init__(self, parent):
        super().__init__(parent, view=pg.PlotItem())

        self.ui.histogram.hide()
        self.ui.roiBtn.hide()
        self.ui.menuBtn.hide()

        plot_view = self.getView()
        plot_view.hideAxis('left')
        plot_view.hideAxis('bottom')

        # 50 frames of 100x100 random noise
        img = np.random.normal(size=(50, 100, 100))
        self.setImage(img)

        text0 = pg.LabelItem("this is a LabelItem", color=(128, 0, 0))
        text0.setPos(25, 25)  # <---- These are coords within the IMAGE
        plot_view.addItem(text0)

        text1 = pg.TextItem(text='This is a TextItem', color=(0, 128, 0))
        plot_view.addItem(text1)
        text1.setPos(75, -20)  # <---- These are coords within the IMAGE

        legend = plot_view.addLegend()
        style = pg.PlotDataItem(pen='w')
        legend.addItem(style, 'legend')


def main():
    app = QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
从PySide2.QtWidgets导入QApplication
从PySide2.QtWidgets导入QMainWindow
从PySide2.QtWidgets导入QWidget
从PySide2.QtWidgets导入QHBoxLayout
将pyqtgraph作为pg导入
将numpy作为np导入
导入系统
类主窗口(QMainWindow):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.cw=QWidget(self)
self.cw.setAutoFillBackground(真)
self.setCentralWidget(self.cw)
self.layout=QHBoxLayout()
self.cw.setLayout(self.layout)
self.DcmImgWidget=MyImageWidget(父项=self)
self.layout.addWidget(self.DcmImgWidget)
self.show()
类MyImageWidget(pg.ImageView):
定义初始化(自身,父级):
super()。\uuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
self.ui.histogram.hide()
self.ui.roiBtn.hide()
self.ui.menuBtn.hide()
plot\u view=self.getView()
plot_view.hideAxis('左')
plot_view.hideAxis('底部')
#50帧100x100随机噪声
img=np.random.normal(大小=(50100100))
self.setImage(img)
text0=pg.LabelItem(“这是一个LabelItem”,颜色=(128,0,0))

text0.setPos(25,25)#一种可能的解决方案是将QLabel添加到ImageView使用的QGraphicsView的视口中:

类MyImageWidget(pg.ImageView):
定义初始化(自身,父级):
super()。\uuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
self.ui.histogram.hide()
self.ui.roiBtn.hide()
self.ui.menuBtn.hide()
plot\u view=self.getView()
绘图视图隐藏轴(“左”)
绘图视图隐藏轴(“底部”)
#50帧100x100随机噪声
img=np.random.normal(大小=(50100100))
self.setImage(img)
label=QLabel(“这是一个QLabel”,self.ui.graphicsView.viewport())
标签。移动(25,25)
再次感谢您!您的解决方案很好地避开了pyqtgraph端。在resizeEvent中更新label.move()方法也会很好地更新相对于角点的位置。