在 PyQt5 中将绘图嵌入到图形视图中
Embeding plot into graphicsView in PyQt5
我使用 pyqtgraph
在 PyQt5 GUI 中绘图。我可以在弹出窗口 window 中绘图,但我正在尝试将图形嵌入 graphicsView
区域。
这是我如何在新的 window 中绘制简单的图:
self.pushButton.clicked.connect(self.btn_clk)
MainWindow.show()
def btn_clk(self):
L = [1,2,3,4,5]
pg.plot(L)
我试过用这条线来嵌入剧情,但是不行:
self.pushButton.clicked.connect(self.btn_clk)
MainWindow.show()
def btn_clk(self):
L = [1,2,3,4,5]
self.graphicsView.plot(L)
完整代码如下:
import pyqtgraph as pg
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(662, 512)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
self.graphicsView.setObjectName("graphicsView")
self.verticalLayout.addWidget(self.graphicsView)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.horizontalLayout.addLayout(self.verticalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 662, 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.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton.clicked.connect(self.btn_clk)
MainWindow.show()
def btn_clk(self):
L = [1,2,3,4,5]
pg.plot(L)#this line plots in a new window
self.graphicsView.plot(L)#this line doesn't work
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_())
Pg.plotreturnspg.graphicsWindows.PlotWindow
class的一个实例,而这个class继承自QGraphicsView
,即pg.graphicsWindows.PlotWindow
用于绘图,但 QGraphicsView
不是。如果您想在 Widget 中嵌入绘图,您必须使用 class pg.PlotWidget()
,您必须更改:
self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
至:
self.graphicsView = pg.PlotWidget(self.centralwidget)
和:
def btn_clk(self):
L = [1,2,3,4,5]
self.graphicsView.plot(L)#this line doesn't work
输出:
我使用 pyqtgraph
在 PyQt5 GUI 中绘图。我可以在弹出窗口 window 中绘图,但我正在尝试将图形嵌入 graphicsView
区域。
这是我如何在新的 window 中绘制简单的图:
self.pushButton.clicked.connect(self.btn_clk)
MainWindow.show()
def btn_clk(self):
L = [1,2,3,4,5]
pg.plot(L)
我试过用这条线来嵌入剧情,但是不行:
self.pushButton.clicked.connect(self.btn_clk)
MainWindow.show()
def btn_clk(self):
L = [1,2,3,4,5]
self.graphicsView.plot(L)
完整代码如下:
import pyqtgraph as pg
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(662, 512)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
self.graphicsView.setObjectName("graphicsView")
self.verticalLayout.addWidget(self.graphicsView)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.horizontalLayout.addLayout(self.verticalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 662, 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.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton.clicked.connect(self.btn_clk)
MainWindow.show()
def btn_clk(self):
L = [1,2,3,4,5]
pg.plot(L)#this line plots in a new window
self.graphicsView.plot(L)#this line doesn't work
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_())
Pg.plotreturnspg.graphicsWindows.PlotWindow
class的一个实例,而这个class继承自QGraphicsView
,即pg.graphicsWindows.PlotWindow
用于绘图,但 QGraphicsView
不是。如果您想在 Widget 中嵌入绘图,您必须使用 class pg.PlotWidget()
,您必须更改:
self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
至:
self.graphicsView = pg.PlotWidget(self.centralwidget)
和:
def btn_clk(self):
L = [1,2,3,4,5]
self.graphicsView.plot(L)#this line doesn't work
输出: