如何在 PyQT5 上更新 QLCDNumber 的值?

Posted

技术标签:

【中文标题】如何在 PyQT5 上更新 QLCDNumber 的值?【英文标题】:How to update value of QLCDNumber on PyQT5? 【发布时间】:2019-12-21 17:17:35 【问题描述】:

我正在尝试更新我的 QLCDNumber,但我失败了好几次。在我的代码中,您可以看到我想从双旋转框中获取输入数字,并将它们显示为我的 LCD 小部件中先前土地利用值的总和。我用 .value() 创建了一个函数,但它不能正常工作。代码有点大,但我突出显示了 QLCDNumber 部分。

此处截图:

作为输入的值

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1039, 654)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
        self.tabWidget.setGeometry(QtCore.QRect(10, 0, 1001, 621))
        self.tabWidget.setObjectName("tabWidget")
        self.tab = QtWidgets.QWidget()
        self.tab.setObjectName("tab")
        self.groupBox = QtWidgets.QGroupBox(self.tab)
        self.groupBox.setGeometry(QtCore.QRect(10, 40, 241, 521))
        font = QtGui.QFont()
        font.setPointSize(11)
        self.groupBox.setFont(font)
        self.groupBox.setTitle("")
        self.groupBox.setObjectName("groupBox")
        self.layoutWidget_5 = QtWidgets.QWidget(self.groupBox)
        self.layoutWidget_5.setGeometry(QtCore.QRect(0, 50, 234, 26))
        self.layoutWidget_5.setObjectName("layoutWidget_5")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.layoutWidget_5)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.grass_label = QtWidgets.QLabel(self.layoutWidget_5)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.grass_label.setFont(font)
        self.grass_label.setObjectName("grass_label")
        self.horizontalLayout_2.addWidget(self.grass_label)
        self.grass_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_5)
        self.grass_value.setSingleStep(0.05)
        self.grass_value.setObjectName("grass_value")
        self.horizontalLayout_2.addWidget(self.grass_value)
        self.layoutWidget_6 = QtWidgets.QWidget(self.groupBox)
        self.layoutWidget_6.setGeometry(QtCore.QRect(0, 80, 234, 26))
        self.layoutWidget_6.setObjectName("layoutWidget_6")
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.layoutWidget_6)
        self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.arable_label = QtWidgets.QLabel(self.layoutWidget_6)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.arable_label.setFont(font)
        self.arable_label.setObjectName("arable_label")
        self.horizontalLayout_3.addWidget(self.arable_label)
        self.arab_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_6)
        self.arab_value.setSingleStep(0.05)
        self.arab_value.setObjectName("arab_value")
        self.horizontalLayout_3.addWidget(self.arab_value)
        self.layoutWidget_7 = QtWidgets.QWidget(self.groupBox)
        self.layoutWidget_7.setGeometry(QtCore.QRect(0, 110, 234, 26))
        self.layoutWidget_7.setObjectName("layoutWidget_7")
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.layoutWidget_7)
        self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.woodl_label = QtWidgets.QLabel(self.layoutWidget_7)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.woodl_label.setFont(font)
        self.woodl_label.setObjectName("woodl_label")
        self.horizontalLayout_4.addWidget(self.woodl_label)
        self.wood_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_7)
        self.wood_value.setSingleStep(0.05)
        self.wood_value.setObjectName("wood_value")
        self.horizontalLayout_4.addWidget(self.wood_value)
        self.layoutWidget_8 = QtWidgets.QWidget(self.groupBox)
        self.layoutWidget_8.setGeometry(QtCore.QRect(0, 140, 234, 26))
        self.layoutWidget_8.setObjectName("layoutWidget_8")
        self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.layoutWidget_8)
        self.horizontalLayout_5.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_5.setObjectName("horizontalLayout_5")
        self.mount_label = QtWidgets.QLabel(self.layoutWidget_8)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.mount_label.setFont(font)
        self.mount_label.setObjectName("mount_label")
        self.horizontalLayout_5.addWidget(self.mount_label)
        self.mount_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_8)
        self.mount_value.setSingleStep(0.05)
        self.mount_value.setObjectName("mount_value")
        self.horizontalLayout_5.addWidget(self.mount_value)
        self.start_butt = QtWidgets.QPushButton(self.groupBox)
        self.start_butt.setGeometry(QtCore.QRect(50, 290, 113, 32))
        self.start_butt.setObjectName("start_butt")
        self.landuse_label = QtWidgets.QLabel(self.groupBox)
        self.landuse_label.setGeometry(QtCore.QRect(20, 0, 201, 21))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(18)
        self.landuse_label.setFont(font)
        self.landuse_label.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.landuse_label.setMidLineWidth(0)
        self.landuse_label.setObjectName("landuse_label")
        self.increase_label = QtWidgets.QLabel(self.groupBox)
        self.increase_label.setGeometry(QtCore.QRect(20, 210, 201, 21))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(18)
        self.increase_label.setFont(font)
        self.increase_label.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.increase_label.setMidLineWidth(0)
        self.increase_label.setObjectName("increase_label")
        self.rainfall_value = QtWidgets.QDoubleSpinBox(self.groupBox)
        self.rainfall_value.setGeometry(QtCore.QRect(50, 240, 110, 24))
        self.rainfall_value.setDecimals(1)
        self.rainfall_value.setSingleStep(1.0)
        self.rainfall_value.setObjectName("rainfall_value")
        self.layoutWidget_9 = QtWidgets.QWidget(self.groupBox)
        self.layoutWidget_9.setGeometry(QtCore.QRect(0, 170, 234, 26))
        self.layoutWidget_9.setObjectName("layoutWidget_9")
        self.horizontalLayout_6 = QtWidgets.QHBoxLayout(self.layoutWidget_9)
        self.horizontalLayout_6.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_6.setObjectName("horizontalLayout_6")

      # Here is the code for the LCD Number
      # -----------------------------------------------------------
        self.total_label = QtWidgets.QLabel(self.layoutWidget_9)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.total_label.setFont(font)
        self.total_label.setObjectName("total_label")
        self.horizontalLayout_6.addWidget(self.total_label)
        self.total_sum = QtWidgets.QLCDNumber(self.layoutWidget_9)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.total_sum.setFont(font)
        self.total_sum.setSegmentStyle(QtWidgets.QLCDNumber.Flat)
        self.total_sum.setProperty("value", 0.0)
        self.total_sum.setObjectName("total_sum")
        self.horizontalLayout_6.addWidget(self.total_sum)
      # -----------------------------------------------------------
        self.layoutWidget_10 = QtWidgets.QWidget(self.groupBox)
        self.layoutWidget_10.setGeometry(QtCore.QRect(0, 20, 234, 26))
        self.layoutWidget_10.setObjectName("layoutWidget_10")
        self.horizontalLayout_7 = QtWidgets.QHBoxLayout(self.layoutWidget_10)
        self.horizontalLayout_7.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_7.setObjectName("horizontalLayout_7")
        self.urban_label = QtWidgets.QLabel(self.layoutWidget_10)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.urban_label.setFont(font)
        self.urban_label.setObjectName("urban_label")
        self.horizontalLayout_7.addWidget(self.urban_label)
        self.urban_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_10)
        self.urban_value.setSingleStep(0.05)
        self.urban_value.setObjectName("urban_value")
        self.horizontalLayout_7.addWidget(self.urban_value)
        self.powered_by_label = QtWidgets.QLabel(self.tab)
        self.powered_by_label.setGeometry(QtCore.QRect(460, 570, 111, 16))
        font = QtGui.QFont()
        font.setPointSize(11)
        self.powered_by_label.setFont(font)
        self.powered_by_label.setObjectName("powered_by_label")
        self.landuse_label_2 = QtWidgets.QLabel(self.tab)
        self.landuse_label_2.setGeometry(QtCore.QRect(330, 10, 421, 21))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(18)
        self.landuse_label_2.setFont(font)
        self.landuse_label_2.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.landuse_label_2.setMidLineWidth(0)
        self.landuse_label_2.setObjectName("landuse_label_2")
        self.hydrograph_widget = QtWidgets.QGroupBox(self.tab)
        self.hydrograph_widget.setGeometry(QtCore.QRect(280, 60, 711, 501))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(14)
        self.hydrograph_widget.setFont(font)
        self.hydrograph_widget.setObjectName("hydrograph_widget")
        self.plot_widget = QtWidgets.QWidget(self.hydrograph_widget)
        self.plot_widget.setGeometry(QtCore.QRect(10, 30, 691, 461))
        self.plot_widget.setObjectName("plot_widget")
        self.tabWidget.addTab(self.tab, "")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1039, 22))
        self.menubar.setObjectName("menubar")
        self.menuMenu = QtWidgets.QMenu(self.menubar)
        self.menuMenu.setObjectName("menuMenu")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionClose = QtWidgets.QAction(MainWindow)
        self.actionClose.setCheckable(False)
        self.actionClose.setObjectName("actionClose")
        self.menuMenu.addAction(self.actionClose)
        self.menubar.addAction(self.menuMenu.menuAction())

        self.retranslateUi(MainWindow)
        self.tabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        MainWindow.setTabOrder(self.urban_value, self.grass_value)
        MainWindow.setTabOrder(self.grass_value, self.arab_value)
        MainWindow.setTabOrder(self.arab_value, self.wood_value)
        MainWindow.setTabOrder(self.wood_value, self.mount_value)
        MainWindow.setTabOrder(self.mount_value, self.rainfall_value)
        MainWindow.setTabOrder(self.rainfall_value, self.start_butt)
        MainWindow.setTabOrder(self.start_butt, self.tabWidget)


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.grass_label.setText(_translate("MainWindow", "Grassland"))
        self.arable_label.setText(_translate("MainWindow", "Arable"))
        self.woodl_label.setText(_translate("MainWindow", "Woodland"))
        self.mount_label.setText(_translate("MainWindow", "Mountain"))
        self.start_butt.setText(_translate("MainWindow", "Start"))
        self.landuse_label.setText(_translate("MainWindow", "Land Use Coverage (%)"))
        self.increase_label.setText(_translate("MainWindow", "Increase in Rainfall (%)"))
        self.total_label.setText(_translate("MainWindow", "Total"))
        self.urban_label.setText(_translate("MainWindow", "Urban"))
        self.powered_by_label.setText(_translate("MainWindow", "Powered by Group 1"))
        self.landuse_label_2.setText(_translate("MainWindow", "Hydrological Modelling for the Ouseburn Catchment"))
        self.hydrograph_widget.setTitle(_translate("MainWindow", "Hydrograph"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "HydroModel v.1.0"))
        self.menuMenu.setTitle(_translate("MainWindow", "Menu"))
        self.actionClose.setText(_translate("MainWindow", "Close"))

#---------------------------------------------------------------------
   # Here is my code for updating the LCDNumber

    def all_inputs(self):    
        urban_inp = self.urban_value.value()
        grass_inp = self.grass_value.value()
        arab_inp = self.arab_value.value()
        wood_inp = self.wood_value.value()
        mount_inp = self.mount_value.value()
        total = urban_inp + grass_inp + arab_inp + wood_inp + mount_inp 
        self.total_sum.display(total)

#---------------------------------------------------------------------

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

【问题讨论】:

嗨 eyllanesc,我使用该函数来收集城市、草地、耕地等值,然后在 QLDCNumber 部分中将它们显示为总和。这是正确的做法吗? 它必须是实时的,这是我现在关心的问题。其余的(增加降雨量和开始按钮)将在稍后设置。我现在要做的是显示每个土地使用价值总和的总数,我的意思是LCD必须最大为100(%)。如果总和大于该计算,则无法显示错误消息。 完全正确。 【参考方案1】:

QDoubleSpinBox::valueChanged(double d)

每当旋转框的值发生变化时,都会发出此信号。

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1039, 654)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
        self.tabWidget.setGeometry(QtCore.QRect(10, 0, 1001, 621))
        self.tabWidget.setObjectName("tabWidget")
        self.tab = QtWidgets.QWidget()
        self.tab.setObjectName("tab")
        self.groupBox = QtWidgets.QGroupBox(self.tab)
        self.groupBox.setGeometry(QtCore.QRect(10, 40, 241, 521))
        font = QtGui.QFont()
        font.setPointSize(11)
        self.groupBox.setFont(font)
        self.groupBox.setTitle("")
        self.groupBox.setObjectName("groupBox")
        self.layoutWidget_5 = QtWidgets.QWidget(self.groupBox)
        self.layoutWidget_5.setGeometry(QtCore.QRect(0, 50, 234, 26))
        self.layoutWidget_5.setObjectName("layoutWidget_5")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.layoutWidget_5)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.grass_label = QtWidgets.QLabel(self.layoutWidget_5)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.grass_label.setFont(font)
        self.grass_label.setObjectName("grass_label")
        self.horizontalLayout_2.addWidget(self.grass_label)
        self.grass_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_5)
        self.grass_value.setSingleStep(0.05)
        self.grass_value.setObjectName("grass_value")
        self.horizontalLayout_2.addWidget(self.grass_value)
        self.layoutWidget_6 = QtWidgets.QWidget(self.groupBox)
        self.layoutWidget_6.setGeometry(QtCore.QRect(0, 80, 234, 26))
        self.layoutWidget_6.setObjectName("layoutWidget_6")
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.layoutWidget_6)
        self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.arable_label = QtWidgets.QLabel(self.layoutWidget_6)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.arable_label.setFont(font)
        self.arable_label.setObjectName("arable_label")
        self.horizontalLayout_3.addWidget(self.arable_label)
        self.arab_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_6)
        self.arab_value.setSingleStep(0.05)
        self.arab_value.setObjectName("arab_value")
        self.horizontalLayout_3.addWidget(self.arab_value)
        self.layoutWidget_7 = QtWidgets.QWidget(self.groupBox)
        self.layoutWidget_7.setGeometry(QtCore.QRect(0, 110, 234, 26))
        self.layoutWidget_7.setObjectName("layoutWidget_7")
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.layoutWidget_7)
        self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.woodl_label = QtWidgets.QLabel(self.layoutWidget_7)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.woodl_label.setFont(font)
        self.woodl_label.setObjectName("woodl_label")
        self.horizontalLayout_4.addWidget(self.woodl_label)
        self.wood_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_7)
        self.wood_value.setSingleStep(0.05)
        self.wood_value.setObjectName("wood_value")
        self.horizontalLayout_4.addWidget(self.wood_value)
        self.layoutWidget_8 = QtWidgets.QWidget(self.groupBox)
        self.layoutWidget_8.setGeometry(QtCore.QRect(0, 140, 234, 26))
        self.layoutWidget_8.setObjectName("layoutWidget_8")
        self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.layoutWidget_8)
        self.horizontalLayout_5.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_5.setObjectName("horizontalLayout_5")
        self.mount_label = QtWidgets.QLabel(self.layoutWidget_8)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.mount_label.setFont(font)
        self.mount_label.setObjectName("mount_label")
        self.horizontalLayout_5.addWidget(self.mount_label)
        self.mount_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_8)
        self.mount_value.setSingleStep(0.05)
        self.mount_value.setObjectName("mount_value")
        self.horizontalLayout_5.addWidget(self.mount_value)
        self.start_butt = QtWidgets.QPushButton(self.groupBox)
        self.start_butt.setGeometry(QtCore.QRect(50, 290, 113, 32))
        self.start_butt.setObjectName("start_butt")
        self.landuse_label = QtWidgets.QLabel(self.groupBox)
        self.landuse_label.setGeometry(QtCore.QRect(20, 0, 201, 21))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(18)
        self.landuse_label.setFont(font)
        self.landuse_label.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.landuse_label.setMidLineWidth(0)
        self.landuse_label.setObjectName("landuse_label")
        self.increase_label = QtWidgets.QLabel(self.groupBox)
        self.increase_label.setGeometry(QtCore.QRect(20, 210, 201, 21))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(18)
        self.increase_label.setFont(font)
        self.increase_label.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.increase_label.setMidLineWidth(0)
        self.increase_label.setObjectName("increase_label")
        self.rainfall_value = QtWidgets.QDoubleSpinBox(self.groupBox)
        self.rainfall_value.setGeometry(QtCore.QRect(50, 240, 110, 24))
        self.rainfall_value.setDecimals(1)
        self.rainfall_value.setSingleStep(1.0)
        self.rainfall_value.setObjectName("rainfall_value")
        self.layoutWidget_9 = QtWidgets.QWidget(self.groupBox)
        self.layoutWidget_9.setGeometry(QtCore.QRect(0, 170, 234, 26))
        self.layoutWidget_9.setObjectName("layoutWidget_9")
        self.horizontalLayout_6 = QtWidgets.QHBoxLayout(self.layoutWidget_9)
        self.horizontalLayout_6.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_6.setObjectName("horizontalLayout_6")

      # Here is the code for the LCD Number
      # -----------------------------------------------------------
        self.total_label = QtWidgets.QLabel(self.layoutWidget_9)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.total_label.setFont(font)
        self.total_label.setObjectName("total_label")
        self.horizontalLayout_6.addWidget(self.total_label)
        self.total_sum = QtWidgets.QLCDNumber(self.layoutWidget_9)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.total_sum.setFont(font)
        self.total_sum.setSegmentStyle(QtWidgets.QLCDNumber.Flat)
        self.total_sum.setProperty("value", 0.0)
        self.total_sum.setObjectName("total_sum")
        self.horizontalLayout_6.addWidget(self.total_sum)
      # -----------------------------------------------------------
        self.layoutWidget_10 = QtWidgets.QWidget(self.groupBox)
        self.layoutWidget_10.setGeometry(QtCore.QRect(0, 20, 234, 26))
        self.layoutWidget_10.setObjectName("layoutWidget_10")
        self.horizontalLayout_7 = QtWidgets.QHBoxLayout(self.layoutWidget_10)
        self.horizontalLayout_7.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_7.setObjectName("horizontalLayout_7")
        self.urban_label = QtWidgets.QLabel(self.layoutWidget_10)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.urban_label.setFont(font)
        self.urban_label.setObjectName("urban_label")
        self.horizontalLayout_7.addWidget(self.urban_label)
        self.urban_value = QtWidgets.QDoubleSpinBox(self.layoutWidget_10)
        self.urban_value.setSingleStep(0.05)
        self.urban_value.setObjectName("urban_value")
        self.horizontalLayout_7.addWidget(self.urban_value)
        self.powered_by_label = QtWidgets.QLabel(self.tab)
        self.powered_by_label.setGeometry(QtCore.QRect(460, 570, 111, 16))
        font = QtGui.QFont()
        font.setPointSize(11)
        self.powered_by_label.setFont(font)
        self.powered_by_label.setObjectName("powered_by_label")
        self.landuse_label_2 = QtWidgets.QLabel(self.tab)
        self.landuse_label_2.setGeometry(QtCore.QRect(330, 10, 421, 21))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(18)
        self.landuse_label_2.setFont(font)
        self.landuse_label_2.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.landuse_label_2.setMidLineWidth(0)
        self.landuse_label_2.setObjectName("landuse_label_2")
        self.hydrograph_widget = QtWidgets.QGroupBox(self.tab)
        self.hydrograph_widget.setGeometry(QtCore.QRect(280, 60, 711, 501))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(14)
        self.hydrograph_widget.setFont(font)
        self.hydrograph_widget.setObjectName("hydrograph_widget")
        self.plot_widget = QtWidgets.QWidget(self.hydrograph_widget)
        self.plot_widget.setGeometry(QtCore.QRect(10, 30, 691, 461))
        self.plot_widget.setObjectName("plot_widget")
        self.tabWidget.addTab(self.tab, "")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1039, 22))
        self.menubar.setObjectName("menubar")
        self.menuMenu = QtWidgets.QMenu(self.menubar)
        self.menuMenu.setObjectName("menuMenu")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionClose = QtWidgets.QAction(MainWindow)
        self.actionClose.setCheckable(False)
        self.actionClose.setObjectName("actionClose")
        self.menuMenu.addAction(self.actionClose)
        self.menubar.addAction(self.menuMenu.menuAction())

        self.retranslateUi(MainWindow)
        self.tabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        MainWindow.setTabOrder(self.urban_value, self.grass_value)
        MainWindow.setTabOrder(self.grass_value, self.arab_value)
        MainWindow.setTabOrder(self.arab_value, self.wood_value)
        MainWindow.setTabOrder(self.wood_value, self.mount_value)
        MainWindow.setTabOrder(self.mount_value, self.rainfall_value)
        MainWindow.setTabOrder(self.rainfall_value, self.start_butt)
        MainWindow.setTabOrder(self.start_butt, self.tabWidget)


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.grass_label.setText(_translate("MainWindow", "Grassland"))
        self.arable_label.setText(_translate("MainWindow", "Arable"))
        self.woodl_label.setText(_translate("MainWindow", "Woodland"))
        self.mount_label.setText(_translate("MainWindow", "Mountain"))
        self.start_butt.setText(_translate("MainWindow", "Start"))
        self.landuse_label.setText(_translate("MainWindow", "Land Use Coverage (%)"))
        self.increase_label.setText(_translate("MainWindow", "Increase in Rainfall (%)"))
        self.total_label.setText(_translate("MainWindow", "Total"))
        self.urban_label.setText(_translate("MainWindow", "Urban"))
        self.powered_by_label.setText(_translate("MainWindow", "Powered by Group 1"))
        self.landuse_label_2.setText(_translate("MainWindow", "Hydrological Modelling for the Ouseburn Catchment"))
        self.hydrograph_widget.setTitle(_translate("MainWindow", "Hydrograph"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "HydroModel v.1.0"))
        self.menuMenu.setTitle(_translate("MainWindow", "Menu"))
        self.actionClose.setText(_translate("MainWindow", "Close"))


class Example(QtWidgets.QMainWindow, Ui_MainWindow):  
    def __init__(self):
        super(Example, self).__init__()
        self.setupUi(self) 

        self.urban_value.valueChanged.connect(self.all_inputs)       # +
        self.grass_value.valueChanged.connect(self.all_inputs)       # +
        self.arab_value.valueChanged.connect(self.all_inputs)        # +
        self.wood_value.valueChanged.connect(self.all_inputs)        # +
        self.mount_value.valueChanged.connect(self.all_inputs)       # +
        
   # Here is my code for updating the LCDNumber
    def all_inputs(self):    
        urban_inp = self.urban_value.value()
        grass_inp = self.grass_value.value()
        arab_inp  = self.arab_value.value()
        wood_inp  = self.wood_value.value()
        mount_inp = self.mount_value.value()
        total = urban_inp + grass_inp + arab_inp + wood_inp + mount_inp 
        self.total_sum.display(total)
        

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Example()
    w.show()
    sys.exit(app.exec_())

【讨论】:

Great S. Nick,您的代码真的很有帮助,解决了我的问题。非常感谢!!

以上是关于如何在 PyQT5 上更新 QLCDNumber 的值?的主要内容,如果未能解决你的问题,请参考以下文章

PyQt5 按钮点击动画

如何更新 PyQt5?

更改 QLcdNumber 文本的颜色 [重复]

PyQt5-Qt Designer简易的数字键盘输入

Qt 使用 QLCDNumber 制作计时器

PyQt5如何使用python在迭代循环中更新单元格值Qtablewidget