我有一个用Python编码的登录窗口,我希望用户在此表单中输入用户名和密码以显示主窗口。实现此功能的最佳方法是什么?
我不知道该怎么做。我正在使用PyQt5。这是我的登录课程。我希望在登录表单中输入正确的用户名和密码后立即显示主类。我已经尝试过这个但是我似乎无法让它发挥作用。
登录类
# Import Statements
from PyQt5 import QtCore, QtGui, QtWidgets
from MainGuiWindow import Ui_MainWindow
# Main Class that holds User Interface Objects
class Ui_loginWindow(object):
# Function for Opening Main GUI window from login window by clicking login button
def openMainWindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.window)
loginWindow.hide()
self.window.show()
def setupUi(self, loginWindow):
loginWindow.setObjectName("loginWindow")
loginWindow.setFixedSize(393, 183)
self.centralwidget = QtWidgets.QWidget(loginWindow)
self.centralwidget.setObjectName("centralwidget")
# username label
self.usernameLabel = QtWidgets.QLabel(self.centralwidget)
self.usernameLabel.setGeometry(QtCore.QRect(10, 20, 84, 19))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.usernameLabel.setFont(font)
self.usernameLabel.setObjectName("usernameLabel")
# password label
self.passwordLabel = QtWidgets.QLabel(self.centralwidget)
self.passwordLabel.setGeometry(QtCore.QRect(10, 70, 83, 19))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.passwordLabel.setFont(font)
self.passwordLabel.setObjectName("passwordLabel")
# login button
self.loginButton = QtWidgets.QPushButton(self.centralwidget)
self.loginButton.setGeometry(QtCore.QRect(280, 110, 101, 27))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.loginButton.setFont(font)
self.loginButton.setObjectName("loginButton")
# username TF
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(100, 20, 141, 20))
self.lineEdit.setObjectName("usernameTF")
# passwordTF
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(100, 70, 141, 20))
self.lineEdit_2.setObjectName("passwordTF")
loginWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(loginWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 393, 21))
self.menubar.setObjectName("menubar")
loginWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(loginWindow)
self.statusbar.setObjectName("statusbar")
loginWindow.setStatusBar(self.statusbar)
self.retranslateUi(loginWindow)
QtCore.QMetaObject.connectSlotsByName(loginWindow)
# Function that sets the text on all the UI Buttons
def retranslateUi(self, loginWindow):
_translate = QtCore.QCoreApplication.translate
loginWindow.setWindowTitle(_translate("loginWindow", "Android SQLite DB Parser Login"))
self.usernameLabel.setText(_translate("loginWindow", "Username:"))
self.passwordLabel.setText(_translate("loginWindow", "Password:"))
self.loginButton.setText(_translate("loginWindow", "Login"))
# login functionality to login to MainGUuiWindow
username = "admin"
password = "password"
def doLogin(self):
username = str(self.ui.lineEdit.text())
password = str(self.ui.lineEdit2.text())
if username == "admin" and password == "password":
self.loginButton.clicked.connect(self.openMainWindow)
else:
QtGui.QMessageBox.warning(self,"Bad username or password", QtGui.QMessageBox.Ok)
# Event Handling Code Section
# Event Handling to open Main GUI Window
self.loginButton.clicked.connect(self.openMainWindow)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
loginWindow = QtWidgets.QMainWindow()
ui = Ui_loginWindow()
ui.setupUi(loginWindow)
loginWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
我们无法为您编写完整的代码,但您可以这样做。尝试以这种方式实现。这可能不是最好的实施方式,但这将解决您的目的。
# main class import statement.
class mainwindow()
def __init__():
# do your main class activity.
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
loginObj = login.login() # login class object import it into your main class code
loginObj.setModal(True) # Model window, use model window, "QDialog"
if not loginObj.succ_login_flag: # set this flag TRUE after success login attempt
sys.exit(0) # exit main class
MainWindow = mainWindow()
MainWindow.show()
sys.exit(app.exec_())