简体   繁体   中英

Embedding matplotlib widget in a QtDesigner generated gui within python program

I'm writing an app to manage and display greenhouse data. Logic is in python, gui in Qt5 created in qt Designer. It is a learning project, I'm new to both. I have: the main_file.py, matplotlib.py (the widget) and a gui.py. Things seem to work, widget loads in gui, no errors in terminal but graph does not show. What am I doing wrong?

# this is my gui.py file below
from Tab_Widget_Main_scailing_01 import Ui_MainWindow


class DesignerMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self, parent = None):
        super(DesignerMainWindow, self).__init__(parent)
        self.setupUi(self)

    def mplwidget(self):
        timestamp = np.array([])
        temp_up = np.array([])
        time_format = '%d,%m,%Y,%X'

        self.ax.plot(timestamp, temp_up)
        self.ax.draw()

if __name__ == '__main__':
    app = QtWidgets.QApplication([sys.argv])
    gui = DesignerMainWindow()
    gui.show()
    sys.exit(app.exec_())

This is a matplotlib.py widget:

from PyQt5 import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

class MplCanvas(FigureCanvas):
    def __init__(self):
        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)
        FigureCanvas.__init__(self, self.fig)
        FigureCanvas.setSizePolicy(self,
                                    QtWidgets.QSizePolicy.Expanding,
                                    QtWidgets.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

class MplWidget(QtWidgets.QWidget):
    def __init__(self, parent = None):
        QtWidgets.QWidget.__init__(self, parent)
        self.canvas = MplCanvas()
        self.vbl = QtWidgets.QVBoxLayout()
        self.vbl.addWidget(self.canvas)
        self.setLayout(self.vbl)

And here lines in the gui.py, designer generated file, pertaining to matplotlib widget:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        (...)
        self.mpl_tab = QtWidgets.QWidget()
        self.mpl_tab.setObjectName("mpl_tab")
        self.horizontalLayout_49 = QtWidgets.QHBoxLayout(self.mpl_tab)
        self.horizontalLayout_49.setObjectName("horizontalLayout_49")
        self.horizontalLayout_48 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_48.setObjectName("horizontalLayout_48")
        self.tabWidget_4 = QtWidgets.QTabWidget(self.mpl_tab)
        self.tabWidget_4.setObjectName("tabWidget_4")
        self.mpl = MplWidget()
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
   sizePolicy.setHeightForWidth(self.mpl.sizePolicy().hasHeightForWidth())
        self.mpl.setSizePolicy(sizePolicy)
        self.mpl.setObjectName("mpl")
        self.tabWidget_4.addTab(self.mpl, "")

Figured it out. The only thing I was missing was "self.mplwidget()" in the main files' __init__() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM