【发布时间】:2016-05-16 15:42:11
【问题描述】:
我一直在开发一个 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()
当我尝试创建主窗口类的 mw 时,我的问题就出现了。出于某种原因,它会执行 MainWindow.initui() 然后立即关闭。我认为它与 main() 函数和 QApplication 对象有关。编写多个窗口并解决此问题的最佳方法是什么?我最初打算为每个窗口创建一个类:passwordPrompt、MainMenu 等,然后实例化每个类的一个实例以加载一个新窗口,但您可以看到它不起作用。
【问题讨论】:
-
嗨@WewLad,您觉得其中一个答案有帮助吗?希望你的问题得到解决:-)
标签: python user-interface pyqt4