1

我是 PyQt 的新手,所以在创建 UI 文件时,我只是复制了一个主窗口 (mainfile.ui) 并将其更改为生成另一个 UI 文件 (Intro.ui)。我知道这不是创建 UI 文件的好方法,因为它总是给出错误:object has no attribute 'exec_'.

这是代码:

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_())

谁能帮我解决这个问题。一旦 python 控制台显示这个AttributeError,内核就会死掉。

4

1 回答 1

5

这个工作正常,感谢您的所有帮助:

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_())
于 2017-09-11T14:56:48.213 回答