简体   繁体   中英

PyQt4_ How to return from MainWindow to LoginWindow?

In the following example how can I close MainWindow and return to LoginWindow after pressing logOutButton ?

import sys
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QDialog, QPushButton, QVBoxLayout, QLineEdit, QMainWindow, QApplication, QMessageBox


class LoginWindow(QDialog):

def __init__(self):
    super().__init__()
    self.build_UI()

def build_UI(self):

    self.username = QLineEdit(self)
    self.username.setPlaceholderText("Username: ")

    self.password = QLineEdit(self)
    self.password.setPlaceholderText("Password: ")
    self.password.setEchoMode(QLineEdit.Normal)

    self.logInButton = QPushButton("Log In")
    self.logInButton.clicked.connect(self.handleLogin)

    layout = QVBoxLayout()

    layout.addWidget(self.username)
    layout.addWidget(self.password)
    layout.addWidget(self.logInButton)

    self.setLayout(layout)

    self.setWindowFlags(Qt.WindowSystemMenuHint)

    self.show()

def handleLogin(self):
    if self.username.text() == "abc" and self.password.text() == "123":  # check credentials
        self.accept()
    else:
        QMessageBox.warning(self, "Error", "Wrong username or password!")


class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()

        self.logOutButton = QPushButton("Log Out", self)
        self.logOutButton.pressed.connect(self.logOut)

        self.show()

    def logOut(self):
        print("log Out")


def run():

    app = QApplication(sys.argv)
    login = LoginWindow()
    if login.exec_() == QDialog.Accepted:
        win = MainWindow()
        sys.exit(app.exec_())


run()

In this case, you want the main window to launch first, but stay hidden during login. For the logout event, just hide the main window and show the login again. I added an Exit button to the login form so the user can exit the app.

Note that I tested this is Qt5.

class LoginWindow(QDialog):

    def __init__(self):
        super().__init__()
        self.build_UI()

    def build_UI(self):

        self.username = QLineEdit(self)
        self.username.setPlaceholderText("Username: ")

        self.password = QLineEdit(self)
        self.password.setPlaceholderText("Password: ")
        self.password.setEchoMode(QLineEdit.Normal)

        self.logInButton = QPushButton("Log In")
        self.logInButton.clicked.connect(self.handleLogin)
        
        self.ExitButton = QPushButton("Exit")  # add exit button to exit app
        self.ExitButton.clicked.connect(self.handleExit)
        
        layout = QVBoxLayout()

        layout.addWidget(self.username)
        layout.addWidget(self.password)
        layout.addWidget(self.logInButton)
        layout.addWidget(self.ExitButton)

        self.setLayout(layout)

        self.setWindowFlags(Qt.WindowSystemMenuHint)

        self.show()

    def handleLogin(self):
        self.accept()

    def handleExit(self):
        sys.exit()  # close app


class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
      
        self.doLogin() # show login button before showing main window

        self.logOutButton = QPushButton("Log Out", self)
        self.logOutButton.pressed.connect(self.logOut)

        self.show()

    def logOut(self):
        self.hide()  # hide main window
        self.doLogin()  # show login
        self.show()
        
    def doLogin(self):
        login = LoginWindow()
        if login.exec_() != QDialog.Accepted:
            self.close()  # exit app
        
def run():
    app = QApplication(sys.argv)
    win = MainWindow()
    sys.exit(app.exec_())

run()

If you want to recreate the main window for each login, you can use a loop in the run function. You will need to add an exit button to the main window if you don't want to return to the login form.

class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
      
        self.logOutButton = QPushButton("Log Out", self)
        self.logOutButton.pressed.connect(self.logOut)

        self.show()

    def logOut(self):
        self.close() # goes back to loop
        
def run():
      app = QApplication(sys.argv)
      while True: # need exit button to exit app
          login = LoginWindow()
          login.exec_()  # will exit app if no login
          win = MainWindow() # recreate main window
          app.exec_()

run()

For checking login credentials:

def run():
      app = QApplication(sys.argv)
      while True: # need exit button to exit app
          loginWindow = LoginWindow()
          if loginWindow.exec_()  == QDialog.Accepted: # user clicked login
              usr = loginWindow.username.text()
              pwd = loginWindow.password.text()
              if usr == "abc" and pwd == "123":  # check credentials
                  win = MainWindow() # recreate main window
                  app.exec_()

One of many solutions (and the most befitting for my purpose):

import sys
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QDialog, QPushButton, QVBoxLayout, QLineEdit, QMainWindow, QApplication, QMessageBox


class LoginWindow(QDialog):

    def __init__(self):
        super().__init__()
        self.build_UI()

    def build_UI(self):

        self.username = QLineEdit(self)
        self.username.setPlaceholderText("Username: ")

        self.password = QLineEdit(self)
        self.password.setPlaceholderText("Password: ")
        self.password.setEchoMode(QLineEdit.Normal)

        self.logInButton = QPushButton("Log In")
        self.logInButton.clicked.connect(self.handleLogin)

        layout = QVBoxLayout()

        layout.addWidget(self.username)
        layout.addWidget(self.password)
        layout.addWidget(self.logInButton)

        self.setLayout(layout)

        self.setWindowFlags(Qt.WindowSystemMenuHint)

        self.show()

    def handleLogin(self):
        if self.username.text() == "a" and self.password.text() == "a":
            self.accept()
        else:
            QMessageBox.warning(self, "Error", "Wrong username or password!")

    def closeEvent(self, event):
        sys.exit()


class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.return_to_login = False

        self.logOutButton = QPushButton("Log Out", self)
        self.logOutButton.pressed.connect(self.logOut)

        self.show()

    def logOut(self):
        self.return_to_login = True
        self.close()

    def closeEvent(self, event):
        if not self.return_to_login:
            sys.exit()


def run():
    app = QApplication(sys.argv)
    while True:
        loginWindow = LoginWindow()
        if loginWindow.exec_() == QDialog.Accepted:
            win = MainWindow()
            app.exec_()


run()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM