在添加小部件时,有关 vispy 的一些问题提到了使用 canvas.native。在 qt 设计器中作为占位符制作的小部件如何用于 vispy?
这个想法是从这个
canvas = vispy.app.Canvas()
w = QMainWindow()
widget = QWidget()
w.setCentralWidget(widget)
widget.setLayout(QVBoxLayout())
widget.layout().addWidget(canvas.native)
widget.layout().addWidget(QPushButton())
w.show()
vispy.app.run()
到一个版本,其中有一个 ui 或生成的 python 文件,该文件有一个名为“frameFor3d”的框架。
class myWindow(QtWidgets.QMainWindow):
def __init__(self):
super(myWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
#declare this here?
canvas = vispy.app.Canvas()
self.ui.frameFor3d.layout().addWidget(canvas.native)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
application = myWindow()
vispy.app.run() #does it go here?
application.show()
sys.exit(app.exec())
这个错误与 None 因为 layout() 是 none。
uic 样本
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'myWindow.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(440, 299)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.frameFor3d = QtWidgets.QFrame(self.centralwidget)
self.frameFor3d.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frameFor3d.setFrameShadow(QtWidgets.QFrame.Raised)
self.frameFor3d.setObjectName("frameFor3d")
self.gridLayout.addWidget(self.frameFor3d, 0, 0, 1, 1)
self.horizontalSlider = QtWidgets.QSlider(self.centralwidget)
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.gridLayout.addWidget(self.horizontalSlider, 1, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 440, 18))
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"))