2

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
user6901947
  • 107
  • 1
  • 1
  • 5
  • Besides, if I don't write the line s.exec_(), the window will not show.However,if I run it in Ipython Console, the kernel will not die – user6901947 Sep 10 '17 at 12:34
  • remove s.exec() and inside WindowIntro __init__: self.widget = QWidget() then self.widget.show() – NewBieBR Sep 11 '17 at 14:13
  • Thank you very much, that works! – user6901947 Sep 11 '17 at 14:14
  • Nice, I'll post an answer then – NewBieBR Sep 11 '17 at 14:37
  • @NewBieBR actually, i solved it by deleting exec(), and replace it with: self.anotherwindow = windowIntro(). Self.anotherwindow.show(). And this works. – user6901947 Sep 11 '17 at 14:40
  • There can be only one exec() – user6901947 Sep 11 '17 at 14:41
  • hmm. So the first WindowIntro() show() doesn't work? – NewBieBR Sep 11 '17 at 14:43
  • Yes, if you don’t add exec() after show(), the windowIntro will not show after the click in the Mainwindow. But using exec(), the Kennel will show an error : object has no attributes exec(), and the kernel will die. This is controversial. I compiled it to exe, and the program will close as soon as I open it. As I have mentioned, exec() is an attribute of the mainwindow. So to solve this problem, I just used the aforementioned methods. – user6901947 Sep 11 '17 at 14:50

1 Answers1

6

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_())
user6901947
  • 107
  • 1
  • 1
  • 5