簡體   English   中英

在 PyQT5 中顯示 vpl.mesh_plot

[英]Displaying vpl.mesh_plot in PyQT5

我需要在用戶界面上呈現 .stl 圖像。

我用 PyQt5 創建了 UI,並且我設法用 vplotlib 渲染了 .stl 圖像。 但是,我在 UI 上的 Qframe 上顯示此 vpl.mesh_plot 時遇到問題(名稱:self.ui.MyQframe;窗口不一定需要是這種類型,也可以是 QGraphicsViewer 或其他)。

這是呈現 .stl 圖像的函數:

import vtkplotlib as vpl
from stl.mesh import Mesh
def setScan(self):
        path, _ = QtWidgets.QFileDialog.getOpenFileName(None, "Select Image", "",
                                                            "stl Files (*.stl)")  # Ask for file
        mesh = Mesh.from_file(path)

        vpl.mesh_plot(path)

        vpl.mesh_plot(mesh)

        vpl.show()

##Edit 1:根據 Eyllanesc 的回答,我將 QFrame 更改為 QtWidget,並在每個 vpl.mesh_ 中設置 fig=self.MyQtWidget ... + 相應地更改了 show()。

但是,它仍然作為一個新窗口打開,我不知道為什么。

def setScan(self):
    path, _ = QtWidgets.QFileDialog.getOpenFileName(None, "Select Image", "",
                                                        "stl Files (*.stl)")  
    mesh = Mesh.from_file(path)
    self.MyQtWidget = vpl.QtFigure()

    vpl.mesh_plot(path,fig=self.MyQtWidget)

    vpl.mesh_plot(mesh,fig=self.MyQtWidget)
    self.MyQtWidget.show()

你必須使用QtFigure ,它是 QWidget:

import sys

import vtkplotlib as vpl
from PyQt5 import QtWidgets
from stl.mesh import Mesh


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

        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)
        vbox = QtWidgets.QVBoxLayout(central_widget)

        button = QtWidgets.QPushButton("Select STL")
        self.figure = vpl.QtFigure()
        vbox.addWidget(button)
        vbox.addWidget(self.figure)

        button.clicked.connect(self.select_filename)
        self.figure.show()

    def select_filename(self):
        path, _ = QtWidgets.QFileDialog.getOpenFileName(
            None, "Select Image", "", "stl Files (*.stl)"
        )
        if path:
            self.load_mesh(path)

    def load_mesh(self, filename):
        mesh = Mesh.from_file(filename)
        vpl.mesh_plot(mesh, fig=self.figure)


app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM