1

我知道这个问题已经以几种方式提出,但我无法在我的具体用例中弄清楚。我使用 QT 设计器(pyQT 5.15)创建布局,并希望使用 vispy 作为显示。阅读后似乎我必须设置一个小部件,但我仍然不清楚如何设置。这就是我所拥有的,我正在尝试通过计时器将颜色从黑色更改为白色的简单示例。没有错误,但也没有可视化。

这是转换为 Python 的 GUI.ui,称为 GUI.py

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(702, 603)
        self.vispy_widget = QtWidgets.QWidget(Dialog)
        self.vispy_widget.setGeometry(QtCore.QRect(150, 20, 531, 531))
        self.vispy_widget.setObjectName("vispy_widget")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(580, 560, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.horizontalScrollBar = QtWidgets.QScrollBar(Dialog)
        self.horizontalScrollBar.setGeometry(QtCore.QRect(160, 560, 401, 20))
        self.horizontalScrollBar.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalScrollBar.setObjectName("horizontalScrollBar")
        self.horizontalSlider = QtWidgets.QSlider(Dialog)
        self.horizontalSlider.setGeometry(QtCore.QRect(20, 60, 111, 16))
        self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider.setObjectName("horizontalSlider")
        self.hist_widget = QtWidgets.QWidget(Dialog)
        self.hist_widget.setGeometry(QtCore.QRect(20, 90, 120, 80))
        self.hist_widget.setObjectName("hist_widget")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.pushButton.setText(_translate("Dialog", "PushButton"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())
```

这是应用程序部分:

    import sys
    from vispy import gloo, app
    from gui import Ui_Dialog
    from PyQt5 import QtWidgets
    app.use_app('pyqt5')

    class Canvas(app.Canvas):

        def __init__(self, *args, **kwargs):
            app.Canvas.__init__(self, *args, **kwargs)
            self._timer = app.Timer('auto', connect=self.on_timer, start=True)
            self.tick = 0

        def on_draw(self, event):
            gloo.clear(color=True)

        def on_timer(self, event):
            self.tick += 1 / 60.0
            c = abs(math.sin(self.tick))
            gloo.set_clear_color((c, c, c, 1))
            self.update()

    class myWindow(QtWidgets.QMainWindow):
        def __init__(self):
            super(myWindow, self).__init__()
            self.ui = Ui_Dialog()
            self.ui.setupUi(self)

            canvas = Canvas()
            self.ui.vispy_widget(canvas.native)


    if __name__ == "__main__":
        gui = QtWidgets.QApplication(sys.argv)
        Dialog = QtWidgets.QDialog()
        ui = Ui_Dialog()
        ui.setupUi(Dialog)
        Dialog.show()
        app.run()
        #sys.exit(app.exec_())
4

1 回答 1

2

有以下错误:

  • self.ui.vispy_widget(canvas.native)命令没有意义,想法是使用 vispy_widget 作为可以通过布局放置的本机 vispy 小部件的容器。

  • .ui 表单的选择用于确定基类,在您的情况下,您应该使用 QDialog 而不是 QMainWindow。

  • 如果您已经在小部件中设置了 Ui_Dialog,则无需在if __name__ == "__main__":.

  • 您必须导入该math模块。

import math
import sys

from vispy import gloo, app

from PyQt5 import QtWidgets

from gui import Ui_Dialog

app.use_app("pyqt5")


class Canvas(app.Canvas):
    def __init__(self, *args, **kwargs):
        app.Canvas.__init__(self, *args, **kwargs)
        self._timer = app.Timer("auto", connect=self.on_timer, start=True)
        self.tick = 0

    def on_draw(self, event):
        gloo.clear(color=True)

    def on_timer(self, event):
        self.tick += 1 / 60.0
        c = abs(math.sin(self.tick))
        gloo.set_clear_color((c, c, c, 1))
        self.update()


class MyWindow(QtWidgets.QDialog):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        self.canvas = Canvas()

        lay = QtWidgets.QVBoxLayout(self.ui.vispy_widget)
        lay.addWidget(self.canvas.native)


if __name__ == "__main__":
    gui = QtWidgets.QApplication(sys.argv)
    w = MyWindow()
    w.show()
    app.run()
于 2021-05-04T00:54:00.230 回答