我一直工作在一個PyQt4的應用程序,這是我到目前爲止有:PyQt4的窗口關閉自動
import sys
from PyQt4 import QtGui, QtCore
class PasswordPrompt(QtGui.QWidget):
def __init__(self):
super(PasswordPrompt, self).__init__()
self.initUi()
def initUi(self):
self.setFixedSize(500, 75)
self.setWindowTitle('Please enter the password...')
self.prompt = QtGui.QLineEdit(self)
self.btn = QtGui.QPushButton('Enter', self)
self.btn.clicked.connect(self.btnClicked)
self.hbox = QtGui.QHBoxLayout()
self.hbox.addWidget(self.prompt)
self.hbox.addWidget(self.btn)
self.vbox = QtGui.QVBoxLayout()
self.vbox.addLayout(self.hbox)
self.vbox2 = QtGui.QVBoxLayout()
self.vbox2.addSpacing(300)
self.hbox2 = QtGui.QHBoxLayout()
self.hbox2.addSpacing(150)
self.vbox2.addLayout(self.hbox2)
self.vbox.addLayout(self.vbox2)
self.setLayout(self.vbox)
self.center()
self.show()
def btnClicked(self):
pw = self.prompt.text()
if pw == "password":
print("Permission granted!")
self.close()
mw = MainWindow()
else:
print("Permissed denied!")
self.prompt.clear()
self.warningText = QtGui.QLabel('That is the wrong password!', self)
self.hbox2.addWidget(self.warningText)
def center(self):
qr = self.frameGeometry()
cp = QtGui.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.initUi()
def initUi(self):
self.setWindowTitle('Main Menu')
self.setFixedSize(1000, 800)
self.show()
def main():
application = QtGui.QApplication(sys.argv)
p = PasswordPrompt()
sys.exit(application.exec())
if __name__=='__main__':
main()
我的問題是當我嘗試創建類主窗口兆瓦。出於某種原因,它會做MainWindow.initui(),然後立即關閉。我認爲它與main()函數和QApplication對象有關。編碼多個窗口並解決此問題的最佳方法是什麼?我原本是要爲每個窗口創建一個類:passwordPrompt,MainMenu等,然後實例化每個類的實例來加載一個新窗口,但是你可以看到它不工作。
嗨@WewLad,你找到一個答案有幫助嗎?希望你的問題解決了:-) –