我正在尝试实现一个 pyvista 情节
import sys
from qtpy import QtWidgets
from qtpy.QtWidgets import QMainWindow
import numpy as np
import pyvista as pv
from pyvistaqt import QtInteractor
class MainWindow(QMainWindow):
def __init__(self, parent=None, show=True):
QtWidgets.QMainWindow.__init__(self, parent)
# create the frame
self.frame = QtWidgets.QFrame()
vlayout = QtWidgets.QVBoxLayout()
# add the pyvista interactor object
self.plotter = QtInteractor(self.frame)
vlayout.addWidget(self.plotter.interactor)
self.frame.setLayout(vlayout)
self.setCentralWidget(self.frame)
# Define a simple linear surface
x = np.array([1,2,3,4,5,6,7,8,9])
y = np.array([1,2,3,4,5,6,7,8,9])
x, y = np.meshgrid(x, y)
z = x*y
self.grid = pv.StructuredGrid(x, y, z)
self.z = z
self.x = x
self.y = y
self.plotter.show_grid()
self.plotter.add_mesh(self.grid, scalars=self.grid.points[:, -1], lighting=True, specular=0.5, smooth_shading=True, show_edges=True,
scalar_bar_args={'vertical': True})
self.plotter.set_scale(xscale=1, yscale=x.ptp()/y.ptp(), zscale=x.ptp()/z.ptp())
self.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec())
进入在 qtdesigner 中创建的 pyqt 选项卡小部件
from PyQt6 import QtCore, QtGui, QtWidgets
from pyvistaqt import QtInteractor
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(0, 0, 801, 571))
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.widget = QtInteractor(self.tab)
self.widget.setGeometry(QtCore.QRect(-11, -1, 811, 551))
self.widget.setObjectName("widget")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec())
但我的尝试导致了各种错误。我不确定这些字段的正确信息是什么
我尝试了各种基类类型和名称,但我真的只是在黑暗中拍摄,因为我还不是很擅长。
即使在我尝试添加任何 pyvistaqt 逻辑之前,它也无法加载基本选项卡小部件模板,从而导致此错误
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-2f4308f7d135> in <module>
46 MainWindow = QtWidgets.QMainWindow()
47 ui = Ui_MainWindow()
---> 48 ui.setupUi(MainWindow)
49 MainWindow.show()
50 sys.exit(app.exec())
<ipython-input-1-2f4308f7d135> in setupUi(self, MainWindow)
14 self.tab = QtWidgets.QWidget()
15 self.tab.setObjectName("tab")
---> 16 self.widget = QtInteractor(self.tab)
17 self.widget.setGeometry(QtCore.QRect(-11, -1, 811, 551))
18 self.widget.setObjectName("widget")
~\anaconda3\lib\site-packages\pyvistaqt\plotting.py in __init__(self, parent, title, off_screen, multi_samples, line_smoothing, point_smoothing, polygon_smoothing, auto_update, **kwargs)
223 qvtk_kwargs[key] = kwargs.pop(key)
224 with _no_base_plotter_init():
--> 225 QVTKRenderWindowInteractor.__init__(self, **qvtk_kwargs)
226 BasePlotter.__init__(self, **kwargs)
227 # backward compat for when we had this as a separate class
~\anaconda3\lib\site-packages\vtkmodules\qt\QVTKRenderWindowInteractor.py in __init__(self, parent, **kw)
258 else:
259 wflags = Qt.WindowFlags()
--> 260 QWidget.__init__(self, parent, wflags | Qt.MSWindowsOwnDC)
261 elif QVTKRWIBase == "QGLWidget":
262 QGLWidget.__init__(self, parent)
TypeError: QWidget(parent: QWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()): argument 1 has unexpected type 'QWidget'
谁能帮我吗?我发现 pyqt 是一个非常复杂且难以理解的库,因为无论如何我都需要将 pyvista 图集成到 pyqt 中,所以我希望让这个图正常工作,这样我就可以玩弄它以试图理解 pyqt 的荒谬性.