将matplotlib小部件嵌入到Python程序中QtDesigner生成的GUI中,可以通过以下步骤实现:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class MatplotlibWidget(QWidget):
def __init__(self, parent=None):
super(MatplotlibWidget, self).__init__(parent)
self.figure = Figure()
self.canvas = FigureCanvas(self.figure)
layout = QVBoxLayout()
layout.addWidget(self.canvas)
self.setLayout(layout)
from PyQt5.uic import loadUi
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
loadUi('your_ui_file.ui', self) # 替换'your_ui_file.ui'为你的UI文件路径
self.matplotlib_widget = MatplotlibWidget()
self.your_qwidget_layout.addWidget(self.matplotlib_widget) # 替换'your_qwidget_layout'为QWidget布局的名称
def plot_graph(self):
ax = self.matplotlib_widget.figure.add_subplot(111)
# 在这里使用matplotlib绘制你的图形
ax.plot([1, 2, 3, 4], [1, 4, 9, 16])
self.matplotlib_widget.canvas.draw()
这样,你就可以将matplotlib小部件嵌入到Python程序中QtDesigner生成的GUI中了。关于matplotlib的更多信息和用法,请参考官方文档。
领取专属 10元无门槛券
手把手带您无忧上云