gpt4 book ai didi

python - 将绘图嵌入到 PyQt5 中的图形 View 中

转载 作者:行者123 更新时间:2023-12-01 02:51:30 26 4
gpt4 key购买 nike

我使用 pyqtgraph 在 PyQt5 GUI 中进行绘图。我可以在弹出窗口中进行绘图,但我正在尝试将图形嵌入到 graphicsView 区域中。

以下是我如何在新窗口中绘制简单的图:

    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.plot返回pg.graphicsWindows.PlotWindow类的实例,该类继承于QGraphicsView,即pg.graphicsWindows.PlotWindow code> 用于绘图,但 QGraphicsView 不是。如果您想在 Widget 中嵌入绘图,则必须使用 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

输出:

enter image description here

关于python - 将绘图嵌入到 PyQt5 中的图形 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44718779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com