我正在编写一个用于管理和显示温室数据的应用程序。 逻辑在python中,在qt Designer中在Qt5中创建gui。 这是一个学习项目,我都是新手。 我有:main_file.py,matplotlib.py(小部件)和gui.py。 事情似乎正常,小部件在gui中加载,终端中没有错误,但图形未显示。我在做什么错了?
# 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_())
这是一个matplotlib.py小部件:
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)
这是设计器生成的gui.py文件中与matplotlib小部件有关的行:
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, "")
答案 0 :(得分:0)
弄清楚了。我唯一缺少的是主文件的__init __()函数中的“ self.mplwidget()”。