从 Qt Designer 调用小部件

Calling a widget from Qt Designer

我创建了下面给出的 QT Designer 代码,其中包含一个名为 mplWidget 的小部件。


from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(741, 542)
        font = QtGui.QFont()
        font.setPointSize(16)
        font.setItalic(True)
        MainWindow.setFont(font)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.main_Group = QtWidgets.QGroupBox(self.centralwidget)
        self.main_Group.setTitle("")
        self.main_Group.setObjectName("main_Group")
        self.graphGroup = QtWidgets.QGroupBox(self.main_Group)
        self.graphGroup.setGeometry(QtCore.QRect(200, 10, 501, 461))
        self.graphGroup.setAlignment(QtCore.Qt.AlignCenter)
        self.graphGroup.setObjectName("graphGroup")
        self.mplWidget = QtWidgets.QWidget(self.graphGroup)
        self.mplWidget.setGeometry(QtCore.QRect(10, 40, 481, 411))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.mplWidget.setFont(font)
        self.mplWidget.setObjectName("mplWidget")
        self.varGroup = QtWidgets.QGroupBox(self.main_Group)
        self.varGroup.setGeometry(QtCore.QRect(10, 10, 181, 251))
        font = QtGui.QFont()
        font.setPointSize(16)
        font.setItalic(True)
        self.varGroup.setFont(font)
        self.varGroup.setAlignment(QtCore.Qt.AlignCenter)
        self.varGroup.setObjectName("varGroup")
        self.pressureLabel = QtWidgets.QLabel(self.varGroup)
        self.pressureLabel.setGeometry(QtCore.QRect(10, 80, 71, 20))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.pressureLabel.setFont(font)
        self.pressureLabel.setObjectName("pressureLabel")
        self.pressureLineEdit = QtWidgets.QLineEdit(self.varGroup)
        self.pressureLineEdit.setGeometry(QtCore.QRect(80, 80, 71, 20))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.pressureLineEdit.setFont(font)
        self.pressureLineEdit.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.pressureLineEdit.setObjectName("pressureLineEdit")
        self.pressureUnitLabel = QtWidgets.QLabel(self.varGroup)
        self.pressureUnitLabel.setGeometry(QtCore.QRect(160, 80, 31, 21))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.pressureUnitLabel.setFont(font)
        self.pressureUnitLabel.setObjectName("pressureUnitLabel")
        self.temperatureLabel = QtWidgets.QLabel(self.varGroup)
        self.temperatureLabel.setGeometry(QtCore.QRect(10, 120, 91, 20))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.temperatureLabel.setFont(font)
        self.temperatureLabel.setObjectName("temperatureLabel")
        self.temperatureLineEdit = QtWidgets.QLineEdit(self.varGroup)
        self.temperatureLineEdit.setGeometry(QtCore.QRect(110, 120, 41, 20))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.temperatureLineEdit.setFont(font)
        self.temperatureLineEdit.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.temperatureLineEdit.setObjectName("temperatureLineEdit")
        self.temperatureUnitLabel = QtWidgets.QLabel(self.varGroup)
        self.temperatureUnitLabel.setGeometry(QtCore.QRect(160, 120, 31, 21))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.temperatureUnitLabel.setFont(font)
        self.temperatureUnitLabel.setObjectName("temperatureUnitLabel")
        self.componentLabel = QtWidgets.QLabel(self.varGroup)
        self.componentLabel.setGeometry(QtCore.QRect(10, 30, 91, 21))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.componentLabel.setFont(font)
        self.componentLabel.setObjectName("componentLabel")
        self.componentCombo = QtWidgets.QComboBox(self.varGroup)
        self.componentCombo.setGeometry(QtCore.QRect(10, 50, 161, 22))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.componentCombo.setFont(font)
        self.componentCombo.setObjectName("componentCombo")
        self.eosLabel = QtWidgets.QLabel(self.varGroup)
        self.eosLabel.setGeometry(QtCore.QRect(10, 160, 141, 20))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.eosLabel.setFont(font)
        self.eosLabel.setObjectName("eosLabel")
        self.eosCombo = QtWidgets.QComboBox(self.varGroup)
        self.eosCombo.setGeometry(QtCore.QRect(11, 180, 161, 22))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.eosCombo.setFont(font)
        self.eosCombo.setObjectName("eosCombo")
        self.variablesSubmitButton = QtWidgets.QPushButton(self.varGroup)
        self.variablesSubmitButton.setGeometry(QtCore.QRect(90, 220, 91, 31))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.variablesSubmitButton.setFont(font)
        self.variablesSubmitButton.setObjectName("variablesSubmitButton")
        self.resultsGroup = QtWidgets.QGroupBox(self.main_Group)
        self.resultsGroup.setGeometry(QtCore.QRect(9, 279, 181, 191))
        self.resultsGroup.setAlignment(QtCore.Qt.AlignCenter)
        self.resultsGroup.setObjectName("resultsGroup")
        self.vrhoLabel = QtWidgets.QLabel(self.resultsGroup)
        self.vrhoLabel.setGeometry(QtCore.QRect(10, 30, 101, 21))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.vrhoLabel.setFont(font)
        self.vrhoLabel.setObjectName("vrhoLabel")
        self.lrhoLabel = QtWidgets.QLabel(self.resultsGroup)
        self.lrhoLabel.setGeometry(QtCore.QRect(10, 90, 111, 21))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.lrhoLabel.setFont(font)
        self.lrhoLabel.setObjectName("lrhoLabel")
        self.vrhoLineEdit = QtWidgets.QLineEdit(self.resultsGroup)
        self.vrhoLineEdit.setGeometry(QtCore.QRect(10, 60, 101, 20))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.vrhoLineEdit.setFont(font)
        self.vrhoLineEdit.setInputMask("")
        self.vrhoLineEdit.setText("")
        self.vrhoLineEdit.setFrame(True)
        self.vrhoLineEdit.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.vrhoLineEdit.setReadOnly(False)
        self.vrhoLineEdit.setObjectName("vrhoLineEdit")
        self.lrhoLineEdit = QtWidgets.QLineEdit(self.resultsGroup)
        self.lrhoLineEdit.setGeometry(QtCore.QRect(10, 120, 101, 20))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.lrhoLineEdit.setFont(font)
        self.lrhoLineEdit.setInputMask("")
        self.lrhoLineEdit.setFrame(True)
        self.lrhoLineEdit.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.lrhoLineEdit.setObjectName("lrhoLineEdit")
        self.lrhoUnitLabel = QtWidgets.QLabel(self.resultsGroup)
        self.lrhoUnitLabel.setGeometry(QtCore.QRect(120, 120, 61, 20))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.lrhoUnitLabel.setFont(font)
        self.lrhoUnitLabel.setObjectName("lrhoUnitLabel")
        self.vrhoUnitLabel = QtWidgets.QLabel(self.resultsGroup)
        self.vrhoUnitLabel.setGeometry(QtCore.QRect(120, 60, 61, 20))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.vrhoUnitLabel.setFont(font)
        self.vrhoUnitLabel.setObjectName("vrhoUnitLabel")
        self.resultsSubmitButton = QtWidgets.QPushButton(self.resultsGroup)
        self.resultsSubmitButton.setGeometry(QtCore.QRect(90, 160, 91, 31))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setItalic(False)
        self.resultsSubmitButton.setFont(font)
        self.resultsSubmitButton.setObjectName("resultsSubmitButton")
        self.horizontalLayout.addWidget(self.main_Group)
        self.verticalLayout.addLayout(self.horizontalLayout)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 741, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Equation of State"))
        self.graphGroup.setTitle(_translate("MainWindow", "Graph"))
        self.varGroup.setTitle(_translate("MainWindow", "Variables"))
        self.pressureLabel.setText(_translate("MainWindow", "Pressure"))
        self.pressureLineEdit.setPlaceholderText(_translate("MainWindow", "101325"))
        self.pressureUnitLabel.setText(_translate("MainWindow", "Pa"))
        self.temperatureLabel.setText(_translate("MainWindow", "Temperature"))
        self.temperatureLineEdit.setPlaceholderText(_translate("MainWindow", "273"))
        self.temperatureUnitLabel.setText(_translate("MainWindow", "K"))
        self.componentLabel.setText(_translate("MainWindow", "Component:"))
        self.eosLabel.setText(_translate("MainWindow", "Equation of State:"))
        self.variablesSubmitButton.setText(_translate("MainWindow", "Calculate"))
        self.resultsGroup.setTitle(_translate("MainWindow", "Results"))
        self.vrhoLabel.setText(_translate("MainWindow", "Vapor Density:"))
        self.lrhoLabel.setText(_translate("MainWindow", "Liquid Density:"))
        self.vrhoLineEdit.setPlaceholderText(_translate("MainWindow", "Initial Guess"))
        self.lrhoLineEdit.setPlaceholderText(_translate("MainWindow", "Initial Guess"))
        self.lrhoUnitLabel.setText(_translate("MainWindow", "<html><head/><body><p>mol/m<span style=\" vertical-align:super;\">3</span></p><p><br/></p></body></html>"))
        self.vrhoUnitLabel.setText(_translate("MainWindow", "<html><head/><body><p>mol/m<span style=\" vertical-align:super;\">3</span></p><p><br/></p></body></html>"))
        self.resultsSubmitButton.setText(_translate("MainWindow", "Calculate"))


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_())

我计划嵌入一个在以下脚本中给出的 matplotlib 图:

import sys
import PyQt5 
from PyQt5 import QtCore, QtGui, uic, QtWidgets
from PyQt5.QtWidgets import QWidget
from EoS_GUIv2 import Ui_MainWindow
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure

if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
    PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)

if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
    PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
    
class MplCanvas(FigureCanvasQTAgg):
    def __init__(self, parent=None, width=5, height=4, dpi = 100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        super(MplCanvas, self).__init__(fig)
    
class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        
        sc = MplCanvas(self, width=5, height=4, dpi=100)
        sc.axes.plot([0,1,2,3,4], [10,1,20,3,40])
        self.ui.mplWidget(sc)
        self.show()
    
        
if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

当我尝试从 Qt Designer 代码调用 mplWidget 时,出现以下错误: 'QWidget' object is not callable问题具体出在

        sc = MplCanvas(self, width=5, height=4, dpi=100)
        sc.axes.plot([0,1,2,3,4], [10,1,20,3,40])
        self.ui.mplWidget(sc)
        self.show()

我真的不知道如何调用我的小部件并嵌入图表。

我正在引用 https://www.learnpyqt.com/courses/graphics-plotting/plotting-matplotlib/ ,但我似乎无法让它工作。

我不明白你为什么要调用 mplWidget。在您的情况下,最可接受的是使用 mplWidget 作为容器并使用布局将 sc 放置在 mplWidget 中:

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        
        sc = MplCanvas(self, width=5, height=4, dpi=100)
        sc.axes.plot([0,1,2,3,4], [10,1,20,3,40])
        
        <b>lay = QtWidgets.QVBoxLayout(self.ui.mplWidget)
        lay.addWidget(sc)</b>

        self.show()