我使用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_())
答案 0 :(得分:2)
Pg.plot返回pg.graphicsWindows.PlotWindow
类的实例,此类继承自QGraphicsView
,即pg.graphicsWindows.PlotWindow
用于绘制,但QGraphicsView
不是。pg.PlotWidget()
。如果你想在Widget中嵌入一个情节,你必须使用类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
和
if(currentSong is paused AND the play button is clicked){
//do something
}else if(currentSong is playing AND pause button is clicked){
//do something else
}
输出: