简体   繁体   English

PyQt4_如何从MainWindow返回LoginWindow?

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

In the following example how can I close MainWindow and return to LoginWindow after pressing logOutButton ?在以下示例中,如何在按下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()

In this case, you want the main window to launch first, but stay hidden during login.在这种情况下,您希望主 window 先启动,但在登录期间保持隐藏。 For the logout event, just hide the main window and show the login again.对于注销事件,只需隐藏主 window 并再次显示登录。 I added an Exit button to the login form so the user can exit the app.我在登录表单中添加了一个退出按钮,以便用户可以退出应用程序。

Note that I tested this is Qt5.请注意,我测试的是 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.如果要为每次登录重新创建主 window,可以在运行 function 中使用循环。 You will need to add an exit button to the main window if you don't want to return to the login form.如果您不想返回登录表单,则需要向主 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()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从对话框窗口中检索数据到pyqt中的主窗口? - How to retrieve data from dialog window to mainwindow in pyqt? PyQT:如何从外部函数访问MainWindow小部件? - PyQT: How to access MainWindow widgets from an external function? PYQT:如何将自定义信号从主窗口(父窗口)发送到子窗口? - PYQT: How to send a custom signal from the mainwindow(parent) to a child window? 如何从 Pyqt5 中没有按钮的 SplashScreen 打开 MainWindow? - How to open MainWindow from a SplashScreen without button in Pyqt5? 如何从对话框按钮到pyqt5中的mainwindow tablewidget获取值? - How to get value from dialog button to mainwindow tablewidget in pyqt5? 如何从 pyqt5 中的父 MainWindow class 继承自身 - How to inherit selfs from parent MainWindow class in pyqt5 从PyQt的主窗口中打开Ui_Form - opening a Ui_Form from a mainwindow in PyQt PyQt5 从另一个访问 MainWindow Window - PyQt5 Access MainWindow from Another Window 如何从属于 MainWindow 类的 QlineEdit 中读取文本,并使用 python 和 pyqt 将其用于 Qthread 类? - How to read text from a QlineEdit which belongs to a MainWindow class, and use it into a Qthread class using python and pyqt? 如何从 MainWindow 调用 QTextEdit 小部件并从另一个具有 QTabWidget 实现的 class 使用它? [pyqt5,pyside,python] - How to call a QTextEdit widget from MainWindow and use it from another class with QTabWidget implementation? [pyqt5, pyside, python]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM