试图在我的 PyQt5 Gui 上绘制图表但无法显示
Trying to have plot a graph on my PyQt5 Gui but cannot get it to show
我正在尝试使用 PyQt5 构建一个小型 GUI,并有一个显示选项卡的可视化选项卡。我做了一些研究,想显示一个简单的图来开始,但我无法在 GUI 上显示它。
我使用 Qt 设计器进行 GUI 布局。
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QVBoxLayout, QAction, QFileDialog,QApplication, QMainWindow
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(789, 640)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setMinimumSize(QtCore.QSize(789, 599))
self.centralwidget.setMaximumSize(QtCore.QSize(789, 599))
self.centralwidget.setObjectName("centralwidget")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(10, 10, 761, 531))
self.widget.setObjectName("widget")
self.gridLayoutWidget = QtWidgets.QWidget(self.widget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(-1, -1, 761, 531))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.frame = QtWidgets.QFrame(self.gridLayoutWidget)
self.frame.setStyleSheet("background-color: rgb(255, 255, 255);")
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 789, 21))
self.menubar.setObjectName("menubar")
self.menuShow_Plot = QtWidgets.QMenu(self.menubar)
self.menuShow_Plot.setObjectName("menuShow_Plot")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionShow_Plot = QtWidgets.QAction(MainWindow)
self.actionShow_Plot.setObjectName("actionShow_Plot")
self.menuShow_Plot.addAction(self.actionShow_Plot)
self.menubar.addAction(self.menuShow_Plot.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menuShow_Plot.setTitle(_translate("MainWindow", "Menu"))
self.actionShow_Plot.setText(_translate("MainWindow", "Show Plot"))
然后添加这个以显示一个简单的情节:
class Sheet(Ui_MainWindow):
def __init__(self):
super().__init__()
def setupUi(self, MainWindow):
super().setupUi(MainWindow)
self.sc = myCanvas()
self.l=QVBoxLayout(self.frame)
self.l.addWidget(self.sc)
self.ToolBar()
def Toolbar(self):
self.actionShow_Plot.triggered.connect(self.Plot)
def Plot(self):
x=[1,2,3,4,5,6,7,8,9,10]
y=[2,4,6,8,10,12,14,16,18,20]
self.sc.plot(x,y)
class myCanvas(FigureCanvas):
def __init__(self):
self.fig = Figure()
FigureCanvas.__init__(self,self.fig)
def plot(self,x,y):
self.fig.clear()
self.ax = self.fig.add_subplot(111)
self.ax.plot(x[1:],y[1:])
self.draw
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_())
每当我单击菜单栏上的 'show plot' 时,都没有任何反应。我如何让它工作?因为我认为我已经按照我所看到的教程中的所有说明进行操作。谢谢
除了@eyllanesc 指出的打字错误,您没有使用您的 class Sheet
这是它不起作用的主要原因之一。
这是一个固定的例子:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QVBoxLayout, QAction, QFileDialog, QApplication, QMainWindow
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.artist import Artist
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(789, 640)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setMinimumSize(QtCore.QSize(789, 599))
self.centralwidget.setMaximumSize(QtCore.QSize(789, 599))
self.centralwidget.setObjectName("centralwidget")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(10, 10, 761, 531))
self.widget.setObjectName("widget")
self.gridLayoutWidget = QtWidgets.QWidget(self.widget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(-1, -1, 761, 531))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setSizeConstraint(
QtWidgets.QLayout.SetDefaultConstraint)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.frame = QtWidgets.QFrame(self.gridLayoutWidget)
self.frame.setStyleSheet("background-color: rgb(255, 255, 255);")
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 789, 21))
self.menubar.setObjectName("menubar")
self.menuShow_Plot = QtWidgets.QMenu(self.menubar)
self.menuShow_Plot.setObjectName("menuShow_Plot")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionShow_Plot = QtWidgets.QAction(MainWindow)
self.actionShow_Plot.setObjectName("actionShow_Plot")
self.menuShow_Plot.addAction(self.actionShow_Plot)
self.menubar.addAction(self.menuShow_Plot.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menuShow_Plot.setTitle(_translate("MainWindow", "Menu"))
self.actionShow_Plot.setText(_translate("MainWindow", "Show Plot"))
class Sheet(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.setUpCanvasUi()
self.connectSingalSlots()
def setUpCanvasUi(self):
self.sc = myCanvas()
self.l = QVBoxLayout(self.ui.frame)
self.l.addWidget(self.sc)
self.ui.centralwidget.setLayout(self.l)
def connectSingalSlots(self):
self.ui.actionShow_Plot.triggered.connect(self.Plot)
def Plot(self):
print("get here")
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
self.sc.plot(x, y)
class myCanvas(FigureCanvas):
def __init__(self, *args, **kwargs):
self.fig = Figure()
super(myCanvas, self).__init__(self.fig, *args, **kwargs)
def plot(self, x, y):
self.fig.clear()
self.ax = self.fig.add_subplot(111)
self.ax.plot(x[1:], y[1:])
self.draw()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
win = Sheet()
win.show()
sys.exit(app.exec_())
仅供参考:您最好看看 this tutorial。
我正在尝试使用 PyQt5 构建一个小型 GUI,并有一个显示选项卡的可视化选项卡。我做了一些研究,想显示一个简单的图来开始,但我无法在 GUI 上显示它。
我使用 Qt 设计器进行 GUI 布局。
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QVBoxLayout, QAction, QFileDialog,QApplication, QMainWindow
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(789, 640)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setMinimumSize(QtCore.QSize(789, 599))
self.centralwidget.setMaximumSize(QtCore.QSize(789, 599))
self.centralwidget.setObjectName("centralwidget")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(10, 10, 761, 531))
self.widget.setObjectName("widget")
self.gridLayoutWidget = QtWidgets.QWidget(self.widget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(-1, -1, 761, 531))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.frame = QtWidgets.QFrame(self.gridLayoutWidget)
self.frame.setStyleSheet("background-color: rgb(255, 255, 255);")
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 789, 21))
self.menubar.setObjectName("menubar")
self.menuShow_Plot = QtWidgets.QMenu(self.menubar)
self.menuShow_Plot.setObjectName("menuShow_Plot")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionShow_Plot = QtWidgets.QAction(MainWindow)
self.actionShow_Plot.setObjectName("actionShow_Plot")
self.menuShow_Plot.addAction(self.actionShow_Plot)
self.menubar.addAction(self.menuShow_Plot.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menuShow_Plot.setTitle(_translate("MainWindow", "Menu"))
self.actionShow_Plot.setText(_translate("MainWindow", "Show Plot"))
然后添加这个以显示一个简单的情节:
class Sheet(Ui_MainWindow):
def __init__(self):
super().__init__()
def setupUi(self, MainWindow):
super().setupUi(MainWindow)
self.sc = myCanvas()
self.l=QVBoxLayout(self.frame)
self.l.addWidget(self.sc)
self.ToolBar()
def Toolbar(self):
self.actionShow_Plot.triggered.connect(self.Plot)
def Plot(self):
x=[1,2,3,4,5,6,7,8,9,10]
y=[2,4,6,8,10,12,14,16,18,20]
self.sc.plot(x,y)
class myCanvas(FigureCanvas):
def __init__(self):
self.fig = Figure()
FigureCanvas.__init__(self,self.fig)
def plot(self,x,y):
self.fig.clear()
self.ax = self.fig.add_subplot(111)
self.ax.plot(x[1:],y[1:])
self.draw
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_())
每当我单击菜单栏上的 'show plot' 时,都没有任何反应。我如何让它工作?因为我认为我已经按照我所看到的教程中的所有说明进行操作。谢谢
除了@eyllanesc 指出的打字错误,您没有使用您的 class Sheet
这是它不起作用的主要原因之一。
这是一个固定的例子:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QVBoxLayout, QAction, QFileDialog, QApplication, QMainWindow
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.artist import Artist
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(789, 640)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setMinimumSize(QtCore.QSize(789, 599))
self.centralwidget.setMaximumSize(QtCore.QSize(789, 599))
self.centralwidget.setObjectName("centralwidget")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(10, 10, 761, 531))
self.widget.setObjectName("widget")
self.gridLayoutWidget = QtWidgets.QWidget(self.widget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(-1, -1, 761, 531))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setSizeConstraint(
QtWidgets.QLayout.SetDefaultConstraint)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.frame = QtWidgets.QFrame(self.gridLayoutWidget)
self.frame.setStyleSheet("background-color: rgb(255, 255, 255);")
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 789, 21))
self.menubar.setObjectName("menubar")
self.menuShow_Plot = QtWidgets.QMenu(self.menubar)
self.menuShow_Plot.setObjectName("menuShow_Plot")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionShow_Plot = QtWidgets.QAction(MainWindow)
self.actionShow_Plot.setObjectName("actionShow_Plot")
self.menuShow_Plot.addAction(self.actionShow_Plot)
self.menubar.addAction(self.menuShow_Plot.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menuShow_Plot.setTitle(_translate("MainWindow", "Menu"))
self.actionShow_Plot.setText(_translate("MainWindow", "Show Plot"))
class Sheet(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.setUpCanvasUi()
self.connectSingalSlots()
def setUpCanvasUi(self):
self.sc = myCanvas()
self.l = QVBoxLayout(self.ui.frame)
self.l.addWidget(self.sc)
self.ui.centralwidget.setLayout(self.l)
def connectSingalSlots(self):
self.ui.actionShow_Plot.triggered.connect(self.Plot)
def Plot(self):
print("get here")
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
self.sc.plot(x, y)
class myCanvas(FigureCanvas):
def __init__(self, *args, **kwargs):
self.fig = Figure()
super(myCanvas, self).__init__(self.fig, *args, **kwargs)
def plot(self, x, y):
self.fig.clear()
self.ax = self.fig.add_subplot(111)
self.ax.plot(x[1:], y[1:])
self.draw()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
win = Sheet()
win.show()
sys.exit(app.exec_())
仅供参考:您最好看看 this tutorial。