[英]PyQt5: Object has no attribute 'exec_' with two main windows
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). 我是PyQt的新手,因此在创建UI文件时,我只是复制了一个Mainwindow(mainfile.ui)并将其更改为生成另一个UI文件(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_'
. 我知道这不是创建UI文件的好方法,因为它总是会出现错误: 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. 一旦python控制台显示此AttributeError
,内核将死亡。
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_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.