[英]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.