PyQT5打开新窗口

时间:2018-08-21 20:06:01

标签: python pyqt5

LoginPage_EXEC文件是一种登录表单,可以正确搜索数据库以找到要登录的用户。理想情况下,下一步是使程序打开一个称为“打开”的新窗口,但我不能这样做。

我已经在LoginPage_EXEC中声明了一个函数,如下所示,以打开在名为MainWindow_Exec的文件中创建的窗口:

def openWindow(self):
    window = MainWindow_EXEC.MainWindow_EXEC()
    window.show()
    self.close()

用户成功登录后,将按以下方式执行此功能:

 reply = msg.exec_()
 if reply == QtWidgets.QMessageBox.Yes:
     print('Yes clicked.')
     self.openWindow()

但是,它不起作用并通过调用MainWindow_EXEC来打开新窗口,该窗口在附加的python文件中创建窗口。有关如何在PYQT中打开此窗口的任何提示。

下面是各个文件的所有代码。感谢您的关注。

LoginPage_EXEC文件

from PyQt5 import QtCore, QtGui, QtWidgets
import LoginPage, MainWindow_Exec, OtherWindow
import sys
import DatabaseConnection

class LoginPage_EXEC():

    def openWindow(self):
        window = MainWindow_EXEC.MainWindow_EXEC()
        window.show()
        self.close()

    def __init__(self):

        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        self.ui = LoginPage.Ui_MainWindow()
        self.ui.setupUi(MainWindow)

        # intialise studentTab
        self.ui.txtStudentUsername.setText("")
        self.ui.txtStudentPassword.setText("")

        self.ui.btnLoginStudent.clicked.connect(self.studentLogin)
        self.ui.btnStudentClose.clicked.connect(self.close)

        # intialise teacherTab
        self.ui.txtTeacherUsername.setText("")
        self.ui.txtTeacherPassword.setText("")

        self.ui.btnLoginTeacher.clicked.connect(self.teacherLogin)
        self.ui.btnCloseTeacher.clicked.connect(self.close)

        MainWindow.show()
        sys.exit(app.exec_())

    def studentLogin(self):
        username = self.ui.txtStudentUsername.text()
        password = self.ui.txtStudentPassword.text()

        if len(DatabaseConnection.studentLogin(username, password)) == 0:
            m = QtWidgets.QMessageBox()
            m.setText("Incorect Username or Password. Please try again?")
            m.setIcon(QtWidgets.QMessageBox.Warning)
            m.setStandardButtons(QtWidgets.QMessageBox.Ok)
            m.setDefaultButton(QtWidgets.QMessageBox.Cancel)
            m.exec_()
        else:
            print(DatabaseConnection.teacherLogin(username, password))
            t = DatabaseConnection.teacherLogin(username, password)
            msg = QtWidgets.QMessageBox()
            firstname = str(t[0][3])
            surname = str(t[0][4])
            message = "you have login in as " + firstname + " " + surname + " is this correct"
            msg.setText(message)
            msg.setIcon(QtWidgets.QMessageBox.Information)
            msg.setStandardButtons(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)

            reply = msg.exec_()
            if reply == QtWidgets.QMessageBox.Yes:
                print('Yes clicked.')
                self.openWindow()

        self.ui.txtStudentUsername.setText("")
        self.ui.txtStudentPassword.setText("")

    def teacherLogin(self):
        username = self.ui.txtTeacherUsername.text()
        password = self.ui.txtTeacherPassword.text()

        if len(DatabaseConnection.teacherLogin(username, password)) == 0:
            msg = QtWidgets.QMessageBox()
            msg.setText("Incorect Username or Password. Please try again?")
            msg.setIcon(QtWidgets.QMessageBox.Warning)
            msg.setStandardButtons(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
            msg.setDefaultButton(QtWidgets.QMessageBox.Cancel)
            msg.exec_()
        else:
            print(DatabaseConnection.teacherLogin(username,password))
            t = DatabaseConnection.teacherLogin(username,password)
            msg = QtWidgets.QMessageBox()
            firstname =str(t[0][3])
            surname = str(t[0][4])
            message = "you have login in as " + firstname + " " + surname + " is this correct"
            msg.setText(message)
            msg.setIcon(QtWidgets.QMessageBox.Information)
            msg.setStandardButtons(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)


            reply = msg.exec_()
            if reply == QtWidgets.QMessageBox.Yes:
                print('Yes clicked.')
                self.openWindow()



        self.ui.txtTeacherUsername.setText("")
        self.ui.txtTeacherPassword.setText("")


    def close(self):
        self.ui.close()

    def IncorrectPassword(self):
        msg = QtWidgets.QMessageBox()
        msg.setIcon(QtWidgets.QMessageBox.Warning)
        msg.setText("This is a message box")
        msg.setStandardButtons(QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel)

if __name__ == "__main__":
    LoginPage_EXEC()

0 个答案:

没有答案