send value to a Qwidget from a different Python class

0

How can I change the value of a Qwidget from another class?

Example:

class Primera(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi('task.ui',self)

        self.label_1.setText('linea1')

class Segunda(QDialog):
    def __int__(self):
        QDialog.__init__(self)
        uic.loadUi('second',self)

        self.boton.clicked.connect(lambda:Primera().label_1.setText('linea1_desde_second_class'))

Change the value of the label in the First class from a button in the Second class.

    
asked by Revsky01 31.07.2018 в 04:33
source

1 answer

1

Classes are abstractions, that is, they describe the generic common behavior of a set of elements, but they are not dynamic elements within the application. The objects that are created using those classes do intervene in the logic of the program.

A program from the point of view of OOP are not a set of files, classes or functions, but it is the interaction between the objects. So your question as it can not be resolved.

Explanation:

every time you call First () you are creating a new widget (object) of type First, and then you are passing the new text to that widget that was created, and since it is inside a function, it will be destroyed just finish executing the function since it is a local variable.

The correct solution assuming that you have created the object of the First class () and in that same place you can access the object created by the Second class ():

# en alguna parte de tu codigo
self.objeto_primero = Primero()
self.objeto_segundo = Segundo()
self.objeto_segundo.boton.clicked.connect(lambda: self.objeto_primero.label_1.setText('linea1_desde_second_class'))

As a recommendation you should review the bases of OOP, PyQt and Qt are based on it and if you do not have that knowledge entrenched you will have much more problems.

    
answered by 31.07.2018 / 04:45
source