gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:16:12 25 4
gpt4 key购买 nike

我是 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,内核就会死掉。

最佳答案

这个很好用,谢谢你的帮助:

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

关于python - PyQt5 : Object has no attribute 'exec_' with two main windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46140708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com