0

Tengo 2 archivos uno.py y dos.py la intencion es que pueda llamar al QMainWindow que esta en uno.py desde dos.py y llamar al QDialog que esta en dos.py desde uno.py

El problema es que al tratar de hacerlo me lanza un error, el error solo muestra que alguna de las 2 clases tiene un conflicto pero no muestra que.

Uno.py

from PyQt5.QtWidgets import QMainWindow,QApplication,QPushButton
from Dos import C_Dos #Esto genera el eror

class Main(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        boton = QPushButton(self)
        boton.setText("Abrir")
        boton.clicked.connect(lambda:self.Abrir())

    def Abrir(self):
        print("ok")
        C_Dos().show()
        self.hide()

Dos.py

from PyQt5.QtWidgets import QDialog,QApplication,QPushButton
from Uno import Main  #Esto genera el error

class C_Dos(QDialog):
    def __init__(self):
        QDialog.__init__(self)

        boton = QPushButton(self)
        boton.setText("Abrir")
        boton.clicked.connect(lambda:self.Abrir())

    def Abrir(self):
        print("ok")
        Main().show()
        self.close()


app = QApplication([])
c = C_Dos()
c.show()
c.resize(500,400)
app.exec_()

Traceback Uno.py

Traceback (most recent call last):
  File "C:\Users\Angel-Dell\Desktop\Uno.py", line 2, in <module>
    from Dos import C_Dos #Esto genera el eror
  File "C:\Users\Angel-Dell\Desktop\Dos.py", line 2, in <module>
    from Uno import Main  #Esto genera el error
  File "C:\Users\Angel-Dell\Desktop\Uno.py", line 2, in <module>
    from Dos import C_Dos #Esto genera el eror
ImportError: cannot import name 'C_Dos'

Process returned 1 (0x1)        execution time : 0.091 s
Presione una tecla para continuar . . .

Traceback Dos.py

Traceback (most recent call last):
  File "C:\Users\Angel-Dell\Desktop\Dos.py", line 2, in <module>
    from Uno import Main  #Esto genera el error
  File "C:\Users\Angel-Dell\Desktop\Uno.py", line 2, in <module>
    from Dos import C_Dos #Esto genera el eror
  File "C:\Users\Angel-Dell\Desktop\Dos.py", line 2, in <module>
    from Uno import Main  #Esto genera el error
ImportError: cannot import name 'Main'

Process returned 1 (0x1)        execution time : 0.089 s
Presione una tecla para continuar . . .
1
  • Cual es el mensaje de error?
    – Vichoko
    el 5 nov. 2019 a las 3:40

1 respuesta 1

2

Tienes un problema de importación cruzada o cíclica. Es como un pez que se muerde la cola. Te recomiendo que des un repaso a este post publicado en Pythond

Una solución puede ser añadir una tercera clase que haga referencia a los dos módulos que quieres importar evitando de esta manera la referencia entre ellos.

Tu código rehecho según esta posible solución puede ser el siguiente:

Uno.py

from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton

class Main(QMainWindow):
    def __init__(self,app):
        QMainWindow.__init__(self)

        boton = QPushButton(self)
        boton.setText("Abrir")
        boton.clicked.connect(app.abrirDialog)

Dos.py

from PyQt5.QtWidgets import QDialog,QPushButton

class C_Dos(QDialog):
    def __init__(self,app):
        QDialog.__init__(self)

        boton = QPushButton(self)
        boton.setText("Abrir")
        boton.clicked.connect(app.abrirMainWindow)

app.py

from PyQt5.QtWidgets import QApplication

#Ninguno de los dos modulos se conoce
from Uno import Main
from Dos import C_Dos

class App():
    def __init__(self):
        self.mainWindow = Main(self)
        self.mainWindow.setWindowTitle('Uno Main Window')
        self.dosDialog = C_Dos(self)
        self.dosDialog.setWindowTitle('Dos Dialog')
        self.mainWindow.show()
        self.mainWindow.resize(500,400)

    def abrirDialog(self):
        print("openning dialog")
        self.dosDialog.show()
        self.dosDialog.resize(500,400)
        print("closing main window")
        self.mainWindow.close()

    def abrirMainWindow(self):
        print("openning main window")
        self.mainWindow.show()
        print("closing dos dialog")
        self.dosDialog.close()

def main():
    qapp = QApplication([])
    app = App()
    qapp.exec_()


if __name__ == "__main__":
    main()

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.