S
S
Sergey Tryapkin2015-07-01 09:30:40
Python
Sergey Tryapkin, 2015-07-01 09:30:40

PyQt 5: condition with QLabel.setText() does not change value on form, how to fix?

Good afternoon!
There is this code:

def function1(self, args=None):
        groupBox = QGroupBox("Титле")
        vbox = QVBoxLayout()
        vbox.addStretch(1)
        groupBox.setLayout(vbox)
        
        integerLabel = QLabel('test')
        vbox.addWidget(integerLabel)
        print('свойство сейчас:' + integerLabel.text())
        if args is not None:
            integerLabel.setText('1')
            print('Тест попадения функции')
            print('Поменялось на:' + args)
        print('свойство сейчас 2:' + integerLabel.text())

The logic is this, in the function1 () function, a QLabel field with the text "test" is created, as well as the args=None argument. Further, there are buttons on the form, when pressed, another function is called, it receives some value inside itself and starts function1 already with args not equal to None, the if condition is triggered. But the .setText condition does not change the value of the QLabel on the form. The console clearly shows that the value has changed, but there is no visual display on the form.
I can not understand what I'm doing wrong :)
Please help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2015-07-09
@Triapkin

Hello!
And where is the "groupBox" added to the window? I made a simple version with a cool one based on QWidget. Everything is displayed as it should.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QGroupBox, QVBoxLayout, QLabel, QPushButton

class Example(QWidget):
    def __init__(self):
        super().__init__()
        l = QVBoxLayout()
        b = QPushButton('Добавить без параметра!')
        b.clicked.connect(lambda: self.function1())
        b2 = QPushButton('Добавить с параметром!')
        b2.clicked.connect(lambda: self.function1(args='параметр'))
        l.addWidget(b)
        l.addWidget(b2)
        self.setLayout(l)
        self.show()

    def function1(self, args=None):
        groupBox = QGroupBox("Титле")
        vbox = QVBoxLayout()
        vbox.addStretch(1)
        groupBox.setLayout(vbox)

        integerLabel = QLabel('test')
        vbox.addWidget(integerLabel)
        print('свойство сейчас:' + integerLabel.text())
        if args is not None:
            integerLabel.setText('1')
            print('Тест попадения функции')
            print('Поменялось на:' + args)
        print('свойство сейчас 2:' + integerLabel.text())
        self.layout().addWidget(groupBox)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Example()
    w.resize(250, 150)
    w.setWindowTitle('Simple')
    w.show()
    sys.exit(app.exec_())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question