Python - PyQt5 : acceder a widgets presentes en otras ventanas (clases)

 
Vista:
sin imagen de perfil
Val: 24
Ha disminuido su posición en 3 puestos en Python (en relación al último mes)
Gráfica de Python

PyQt5 : acceder a widgets presentes en otras ventanas (clases)

Publicado por Yago (12 intervenciones) el 02/04/2020 11:01:10
Muy buenos días.

Estoy elaborando un programa de uso personal con PyQt5 en Python que usa diversas pantallas (clases QWidget) y que las gestion a traves de un controlador. He elaborado un pequeño fragmento de código para poder abordar la cuestión, ya que el programa original es demasiado largo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
 
class Window (QWidget): ##########################################
 
    S_configuration = pyqtSignal()
 
    def __init__(self):
        super().__init__()
        self.initUI()
 
    def initUI(self):
        self.Btt1 = QPushButton('Generate')
        self.Btt2 = QPushButton('Config')
 
        self.content = QHBoxLayout(self)
 
        self.content.addWidget(self.Btt1)
        self.content.addWidget(self.Btt2)
 
        self.setLayout(self.content)
 
 
        self.Btt2.clicked.connect(self.FBtt2)
 
        self.Btt1.clicked.connect(self.FBtt1)
 
    def FBtt1 (self):
        print('Esto no funciona')
 
    def FBtt2 (self):
        self.S_configuration.emit()
 
class configuration (QWidget): ##########################################
 
    S_back = pyqtSignal()
 
    def __init__(self):
        super().__init__()
        self.initUI()
 
    def initUI(self):
 
        self.LE = QLineEdit(self)
        self.Btt = QPushButton('Go back')
 
        self.content = QHBoxLayout(self)
 
        self.content.addWidget(self.LE)
        self.content.addWidget(self.Btt)
 
        self.setLayout(self.content)
 
        self.Btt.clicked.connect(self.FBtt)
 
    def FBtt (self):
 
        self.S_back.emit()
 
class Controller(): ##########################################
 
    def __init__(self):
 
        self.Window = Window()
        self.configuration = configuration()
 
    def show_Window(self):
 
        self.configuration.close()
        self.Window.S_configuration.connect(self.show_configuration)
        self.Window.show()
 
    def show_configuration(self):
 
        self.Window.close()
        self.configuration.S_back.connect(self.show_Window)
        self.configuration.show()
 
def main():
    app = QApplication(sys.argv)
    controller = Controller()
    controller.show_Window()
    sys.exit(app.exec_())
 
if __name__ == '__main__':
    main()

Este programa conecta dos pantallas. La primera tiene los botones generar y configuración. El segundo nos lleva a una pantalla donde se puede introducir texto en un QLineEdit. Me gustaría que al escribir texto y volver a la pantalla inicial a través del botón 'Go back', pudieramos acceder de algún modo al texto introducido.

Para ilustrarlo, en este caso me gustaria que el botón de la primera ventana, en vez de devolver el string 'Esto no funciona' devolviera el contenido del QLineEdit ya mencionado.

Gracias de antemano.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 2.808
Oro
Ha mantenido su posición en Python (en relación al último mes)
Gráfica de Python

PyQt5 : acceder a widgets presentes en otras ventanas (clases)

Publicado por tincopasan (1082 intervenciones) el 02/04/2020 16:21:03
Hola: espero haber entendido tu consulta,en base a tu ejemplo es muy simple:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
 
############################################
 
class Window (QWidget):
 
    S_configuration = pyqtSignal()
 
    def __init__(self):
        super().__init__()
        self.initUI()
 
    def initUI(self):
        self.Btt1 = QPushButton('Generate')
        self.Btt2 = QPushButton('Config')
 
        self.content = QHBoxLayout(self)
 
        self.content.addWidget(self.Btt1)
        self.content.addWidget(self.Btt2)
 
        self.setLayout(self.content)
 
 
        self.Btt2.clicked.connect(self.FBtt2)
 
        self.Btt1.clicked.connect(self.FBtt1)
 
    def FBtt1 (self):
        print('hola',configuration.texto)
 
    def FBtt2 (self):
        self.S_configuration.emit()
 
class configuration (QWidget):
    texto="vacio"   #hay que dejar vacía la cadena, lo deje para que veas que pasa sino pasas por configuración.
    S_back = pyqtSignal()
 
    def __init__(self):
        super().__init__()
        self.initUI()
 
    def initUI(self):
 
        self.LE = QLineEdit(self)
        self.Btt = QPushButton('Go back')
 
        self.content = QHBoxLayout(self)
 
        self.content.addWidget(self.LE)
        self.content.addWidget(self.Btt)
 
        self.setLayout(self.content)
 
        self.Btt.clicked.connect(self.FBtt)
 
    def FBtt (self):
 
        configuration.texto = self.LE.text()
 
        self.S_back.emit()
 
class Controller():
 
    def __init__(self):
 
        self.Window = Window()
        self.configuration = configuration()
 
    def show_Window(self):
 
        self.configuration.close()
        self.Window.S_configuration.connect(self.show_configuration)
        self.Window.show()
 
    def show_configuration(self):
 
        self.Window.close()
        self.configuration.S_back.connect(self.show_Window)
        self.configuration.show()
 
def main():
    app = QApplication(sys.argv)
    controller = Controller()
    controller.show_Window()
    sys.exit(app.exec_())
 
if __name__ == '__main__':
    main()
Si no es eso lo que buscas, perdón.
Saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar