且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 PyQt4 中动态更改 QLabel 文本

更新时间:2023-01-28 15:28:21

您没有正确使用 pyuic 创建的模块.您不应该直接编辑这些模块 - 它们应该导入到您的主应用程序中.

You are not using the modules created by pyuic correctly. You should never directly edit these modules - they should imported into your main application.

pyuic 生成的 UI 类有一个 setupUi 方法.此方法采用您在 Qt 设计器中创建的***类的实例,并将设计器中的所有小部件添加到该实例中.例如,label_nombre 将成为传递给setupUi 的实例的属性.通常,您会创建***类的子类,然后将self 作为实例传入(见下文).

The UI classes that pyuic generates have a setupUi method. This method takes an instance of the top-level class that you created in Qt Designer, and it will add all the widgets from designer to that instance. So label_nombre, for example, would become an attribute of the instance passed to setupUi. Usually, you will create a subclass of the top-level class, and then pass in self as the instance (see below).

我建议您使用 pyuic 重新生成 ui 文件并将它们另存为,例如 dialog_ui.pydashboard_ui.py.

I would suggest that you re-generate your ui files with pyuic and save them as, say dialog_ui.py and dashboard_ui.py.

你的程序结构会变成这样:

The structure of your program would then become something like this:

from PyQt4 import QtCore, QtGui
from dashboard_ui import Ui_dashboard
from dialog_ui import Ui_Dialog

class logica_login(QtGui.QDialog, Ui_Dialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        self.setupUi(self)
        self.bloguin_aceptar.clicked.connect(self.validacion)
        self.blogin_cancelar.clicked.connect(self.reject)

    def validacion(self):
        ...

class logica_tablero(QtGui.QMainWindow, Ui_dashboard):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setupUi(self)
        self.label_nombre.setText("hola")
        ...

if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    login = logica_login()
    if login.exec_() == QtGui.QDialog.Accepted:
        dashboard = logica_tablero()
        dashboard.showMaximized()
        sys.exit(app.exec_())