PyQt5: Object has no attribute 'exec_' with two main windows

13.8k views Asked by At

I am new to PyQt, so when I am creating UI files, I just copied one Mainwindow (mainfile.ui) and changed it to produce another UI file (Intro.ui). I know this is not a good way to create UI files, as it always gives the error: object has no attribute 'exec_'.

Here is the code:

MainFile = "mainfile.ui"

Ui_MainWindow, QtBaseClass = uic.loadUiType(MainFile)

FileIntro = "Intro.ui" 

Ui_WindowIntro,_ = uic.loadUiType(FileIntro)

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.ButtonIntro.clicked.connect(self.OpenWindowIntro)
    def OpenWindowIntro(self):
        s = WindowIntro()
        s.show()
        s.exec_() #here is the problem.

class WindowIntro(QtWidgets.QMainWindow, Ui_WindowIntro):

    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_WindowIntro.__init__(self)
        self.setupUi(self)
        #close the window
        self.Button2.clicked.connect(self.Close)

    def Close(self):
        self.close()

if __name__ == "__main__":

    app = 0 # if not the core will die

    app = QtWidgets.QApplication(sys.argv)

    if login():

        window = MainWindow()

        window.show()

        sys.exit(app.exec_())

Can anyone help me to solve this problem. Once the python console shows this AttributeError, the kernel will die.

1

There are 1 answers

0
user6901947 On

This one works fine,thanks for all you help:

from PyQt5 import QtGui, QtCore, QtWidgets

MainFile = "mainfile.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(MainFile)
FileIntro = "Intro.ui" 
Ui_WindowIntro,_ = uic.loadUiType(FileIntro)

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.ButtonIntro.clicked.connect(self.OpenWindowIntro)

    def OpenWindowIntro(self):
        self.anotherwindow = WindowIntro()
        self.anotherwindow.show()

class WindowIntro(QtWidgets.QMainWindow, Ui_WindowIntro):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_WindowIntro.__init__(self)
        self.setupUi(self)

        #close the window
        self.Button2.clicked.connect(self.Close)

    def Close(self):
        self.close()

if __name__ == "__main__":
    app = 0 # if not the core will die
    app = QtWidgets.QApplication(sys.argv)

    if login():
        window = MainWindow()
        window.show()
        sys.exit(app.exec_())