Updating label text from another class

Viewed 465

I'm new at python and i need some help ;-)

I created a window with a label with the QT designer en generated the py file (window.py):

'''

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(847, 283)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lblMain = QtWidgets.QLabel(self.centralwidget)
        self.lblMain.setGeometry(QtCore.QRect(160, 60, 311, 51))
        self.lblMain.setObjectName("lblMain")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 847, 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", "MainWindow"))
        self.lblMain.setText(_translate("MainWindow", "label main window"))

'''

I created main.py which calls the window:

from PyQt5 import QtWidgets,QtGui
from window import Ui_MainWindow

class window(QtWidgets.QMainWindow):

    def __init__(self):
        super(window, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)


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

So far so good.

From within main.py i can set the text of the label using:

self.ui.lblMain.setText('some text')

This works also.

Now i would like to create another file with another class which can update the label. class update.py:

from PyQt5 import QtWidgets,QtGui
from window import Ui_MainWindow

class window(QtWidgets.QMainWindow):
    def __init__(self):
        super(window, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)


    def settext(self):
        self.ui.lblMain.setText('updated')

And here i'm stuck. Can anyone give a hand ?

Cheers John

1 Answers

A function to update a label no matter where it came from:

def update_label(label, new_text):
        label.setText(new_text)

You can save this function anywhere you like including inside a new class. The function or the new class don't need to know anything about the ui to do this.

If it's in a class you'll have to create an instance of that class before using it.

The class you show should work perfectly, simply call the function settext somewhere.

Under update.py:

class Updater:
    def __init__(self, label):
        self.label = label

    def settext(self):
        self.label.setText('updated')

Notice that this class is not another window, it accepts the label object from the Ui_MainWindow class as an argument and saves it as a property. To use it add under main, after you create the app:

my_instance = Updater(win.ui.lblMain)
my_instance.settext()

You could even pass the whole win.ui as an argument. As long as all the classes share the same instance of ui then they will change the same widgets

For completeness, a full program that uses the updater class to change the text in the GUI:

from PyQt5 import QtWidgets,QtGui
from window import Ui_MainWindow


class window(QtWidgets.QMainWindow):

    def __init__(self):
        super(window, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)


class Updater:
    def __init__(self, label):
        self.label = label

    def settext(self):
        self.label.setText('updated')


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    win = window()
    win.show()
    
    my_instance = Updater(win.ui.lblMain)
    my_instance.settext()
    
    sys.exit(app.exec_())
Related