WoHowLearn的博客

432987409 (入群先看 本群须知 ) 微信公众号:WoHowLearn

marplotlib 窗口在 pyqt5 frame窗体中显示(实例化法 , 不推荐)

参考文章:http://www.cnblogs.com/hhh5460/p/4322652.html
UI文件  Ui_MainWindow.py
# -*- coding: utf-8 -*-

# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.tabWidget = QtWidgets.QTabWidget(self.centralWidget)
        self.tabWidget.setGeometry(QtCore.QRect(30, 30, 721, 441))
        self.tabWidget.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.tabWidget.setObjectName("tabWidget")
        self.tab = QtWidgets.QWidget()
        self.tab.setObjectName("tab")
        self.frame = QtWidgets.QFrame(self.tab)
        self.frame.setGeometry(QtCore.QRect(90, 160, 321, 141))
        self.frame.setStyleSheet("background-color: rgb(255, 170, 0);")
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.tabWidget.addTab(self.tab, "")
        self.tab_2 = QtWidgets.QWidget()
        self.tab_2.setObjectName("tab_2")
        self.tabWidget.addTab(self.tab_2, "")
        MainWindow.setCentralWidget(self.centralWidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))

逻辑文件
from Ui_connect import Ui_MainWindow
#pyqt类
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from numpy import arange, sin, pi
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class  Function( Ui_MainWindow, QMainWindow):
    def __init__(self, parent=None):
        super(Function, self).__init__(parent)
        self.setupUi(self)
        self.frame=MyStaticMplCanvas(self.frame)#关键点

class MyMplCanvas(FigureCanvas):
    """这是一个窗口部件,即QWidget(当然也是FigureCanvasAgg)"""
    def __init__(self, parent=None, width=4, height=1.7, dpi=80):
        fig = Figure(figsize=(width, height), dpi=dpi)#设置画布大小
        self.axes = fig.add_subplot(111)
        # 每次plot()调用的时候,我们希望原来的坐标轴被清除(所以False)
        self.axes.hold(False)

        self.compute_initial_figure()

        #
        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self,
                                   QSizePolicy.Expanding,
                                   QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    def compute_initial_figure(self):
        pass
class MyStaticMplCanvas(MyMplCanvas):
    """静态画布:一条正弦线"""
    def compute_initial_figure(self):
        t = arange(0.0, 3.0, 0.01)
        s = sin(2*pi*t)
        self.axes.plot(t, s)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = Function()#关键点
    MainWindow.show()
    sys.exit(app.exec_())




            
阅读更多
个人分类: python3.5&pyqt5
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

marplotlib 窗口在 pyqt5 frame窗体中显示(实例化法 , 不推荐)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭