【问题标题】:Change the text of a label after clicking a button单击按钮后更改标签的文本
【发布时间】:2019-09-14 15:01:28
【问题描述】:

我不明白为什么我的标签文本在单击按钮后没有更新。如果我想在控制台上显示输出,没有问题。

import sys
from qtpy import QtWidgets
from UI.mainwindow import Ui_MainWindow

app = QtWidgets.QApplication(sys.argv)

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.calc_btn.clicked.connect(self.on_calc_btn_click)

    def on_calc_btn_click(self):
        weight = int(self.ui.weight_textbox.text())
        height = int(self.ui.height_textbox.text())
        bmi = weight/height**2

        print(str(bmi)) # -> works
        self.ui.bmi_label.setText(str(bmi)) # -> label does not update


window = MainWindow()
window.show()
sys.exit(app.exec_())

【问题讨论】:

  • 分享Ui_MainWindow班级
  • UI_MainWindow 类是 QT 自动生成的。这里只设置了默认样式
  • 那么分享吧,因为根据你展示的逻辑应该没有问题
  • 在PyQt5中可以正常工作,点击按钮后快捷方式的文本会更新。
  • @Baflora 正如 S.Nick 指出您的代码工作正常,您有任何错误消息吗?您使用的是什么 qtpy 后端?你的操作系统是什么?如果你使用 qtpy 作为 PyQt5 后端,那么你有什么版本的 PyQt5?

标签: python pyqt pyqt5 qt-designer


【解决方案1】:
#Try This

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)
class Ui_CalcBMI(object):
    def setupUi(self, CalcBMI):
        CalcBMI.setObjectName(_fromUtf8("CalcBMI"))
        CalcBMI.resize(400, 300)
        self.pushButton = QtGui.QPushButton(CalcBMI)
        self.pushButton.setGeometry(QtCore.QRect(140, 220, 111, 28))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton.clicked.connect(self.On_Update_Clicked)
        #add textbox here to get height and weight
        self.label = QtGui.QLabel(CalcBMI)
        self.label.setGeometry(QtCore.QRect(150, 70, 131, 101))
        self.label.setObjectName(_fromUtf8("label"))
        self.retranslateUi(CalcBMI)
    def On_Update_Clicked(self):
        #Get values from textbox and calculate bmi here
        bmi="23.5"
        self.label.setText(bmi)
    def retranslateUi(self, CalcBMI):
        CalcBMI.setWindowTitle(_translate("CalcBMI", "CalcBMI", None))
        self.pushButton.setText(_translate("CalcBMI", "Calculate now", None))
        self.label.setText(_translate("CalcBMI", "inProgess", None))
if __name__ == "__main__":
    import sys
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_X11InitThreads)
    app = QtGui.QApplication(sys.argv)
    CalcBMI = QtGui.QWidget()
    ui = Ui_CalcBMI()
    ui.setupUi(CalcBMI)
    CalcBMI.show()
    sys.exit(app.exec_())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 2016-02-29
    • 2022-12-04
    相关资源
    最近更新 更多