弹出窗口失败

时间:2018-03-16 15:08:39

标签: python pyqt5

我正在尝试创建一个登录界面,当我点击"登录"如果信息正确,将进入另一个窗口。主窗口和对话框可以单独显示,但在分组后,总是弹出对话框失败。这个问题困扰了我很长一段时间,如果有人可以回答我,我将非常感激,很高兴,非常感谢。这是我的代码:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import *


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(640, 480)
        font = QtGui.QFont()
        font.setFamily("Bell MT")
        font.setPointSize(12)
        MainWindow.setFont(font)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(60, 120, 131, 31))
        font = QtGui.QFont()
        font.setFamily("Jokerman")
        font.setPointSize(16)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(70, 190, 111, 20))
        font = QtGui.QFont()
        font.setFamily("Jokerman")
        font.setPointSize(16)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.user_name = QtWidgets.QLineEdit(self.centralwidget)
        self.user_name.setGeometry(QtCore.QRect(200, 120, 301, 41))
        self.user_name.setObjectName("user_name")
        self.password = QtWidgets.QLineEdit(self.centralwidget)
        self.password.setGeometry(QtCore.QRect(200, 180, 301, 41))
        self.password.setEchoMode(QtWidgets.QLineEdit.Password)
        self.password.setObjectName("password")
        self.login = QtWidgets.QPushButton(self.centralwidget)
        self.login.setGeometry(QtCore.QRect(300, 260, 101, 51))
        font = QtGui.QFont()
        font.setFamily("Jokerman")
        font.setPointSize(16)
        self.login.setFont(font)
        self.login.setObjectName("login")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(240, 30, 191, 31))
        font = QtGui.QFont()
        font.setFamily("Jokerman")
        font.setPointSize(18)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.login.clicked.connect(self.accept)
        self.login.clicked.connect(MainWindow.close)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "user name:"))
        self.label_2.setText(_translate("MainWindow", "password:"))
        self.login.setText(_translate("MainWindow", "login"))
        self.label_3.setText(_translate("MainWindow", "Market storage"))

    def accept(self):
        if self.user_name.text() == "123" and self.password.text() == "123":
            app = QApplication(sys.argv)
            dialog = QDialog()
            ui = Ui_Dialog()
            ui.setupUi(dialog)
            dialog.show()
            sys.exit(app.exec_())
        else:
            print("wrong imformation")


class Ui_Dialog(QDialog):
    def __init__(self, parent=None):
        super(Ui_Dialog, self).__init__(parent)

    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(640, 480)
        self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(-50, 250, 621, 31))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel | QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(210, 140, 291, 81))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(20)
        self.label.setFont(font)
        self.label.setObjectName("label")

        self.retranslateUi(Dialog)
        self.buttonBox.accepted.connect(Dialog.accept)
        self.buttonBox.rejected.connect(Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "the second windows"))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

问题主要是因为您只需要QApplication,所以当您创建另一个时,您会遇到意外行为。此外,该任务快速结束,因此当dialog删除局部变量时,窗口也会关闭,因此不会发生这种情况,您必须成为该类的dialog成员。 / p>

def accept(self):
    if self.user_name.text() == "123" and self.password.text() == "123":
        self.dialog = QDialog()
        ui = Ui_Dialog()
        ui.setupUi(self.dialog)
        self.dialog.show()
    else:
        print("wrong imformation")