簡體   English   中英

PyQt4_如何從MainWindow返回LoginWindow?

[英]PyQt4_ How to return from MainWindow to LoginWindow?

在以下示例中,如何在按下logOutButton后關閉MainWindow並返回LoginWindow

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()

在這種情況下,您希望主 window 先啟動,但在登錄期間保持隱藏。 對於注銷事件,只需隱藏主 window 並再次顯示登錄。 我在登錄表單中添加了一個退出按鈕,以便用戶可以退出應用程序。

請注意,我測試的是 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()

如果要為每次登錄重新創建主 window,可以在運行 function 中使用循環。 如果您不想返回登錄表單,則需要向主 window 添加退出按鈕。

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()

檢查登錄憑據:

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_()

許多解決方案之一(也是最適合我的目的):

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM