我有2个PyQt5文件-一个登录窗口和一个欢迎窗口。成功登录后,我需要弹出欢迎窗口。欢迎窗口中的按钮应该可以将我带回到登录窗口。当我按下相同按钮时,请任何人帮助我隐藏欢迎窗口。
请在下面找到代码
login.py
from PyQt5 import QtCore, QtGui, QtWidgets import sqlite3 import os from signup import Ui_Dialog2 from welcome import welcomeClass os.chdir(r"C:\Users\maanto2\Desktop\qt") class Ui_Dialog(object): def showMessageBox(self,title,message): msgBox =QtWidgets.QMessageBox() msgBox.setIcon(QtWidgets.QMessageBox.Warning) msgBox.setWindowTitle(title) msgBox.setText(message) msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok) msgBox.exec_() def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(428, 258) self.user_name = QtWidgets.QLabel(Dialog) self.user_name.setGeometry(QtCore.QRect(40, 50, 61, 16)) self.user_name.setObjectName("user_name") self.pass_word = QtWidgets.QLabel(Dialog) self.pass_word.setGeometry(QtCore.QRect(40, 90, 61, 16)) self.pass_word.setObjectName("pass_word") self.push_login = QtWidgets.QPushButton(Dialog) self.push_login.setGeometry(QtCore.QRect(150, 160, 91, 41)) self.push_login.setObjectName("push_login") self.push_sign = QtWidgets.QPushButton(Dialog) self.push_sign.setGeometry(QtCore.QRect(270, 160, 81, 41)) self.push_sign.setObjectName("push_sign") self.user_lineEdit = QtWidgets.QLineEdit(Dialog) self.user_lineEdit.setGeometry(QtCore.QRect(150, 50, 221, 20)) self.user_lineEdit.setObjectName("user_lineEdit") ####### self.push_login.clicked.connect(self.loginCheck) ####### self.pass_lineEdit = QtWidgets.QLineEdit(Dialog) self.pass_lineEdit.setGeometry(QtCore.QRect(150, 90, 221, 20)) self.pass_lineEdit.setObjectName("pass_lineEdit") ####### self.push_sign.clicked.connect(self.signCheck) ####### self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Login Window")) self.user_name.setText(_translate("Dialog", "USERNAME")) self.pass_word.setText(_translate("Dialog", "PASSWORD")) self.push_login.setText(_translate("Dialog", "LOGIN")) self.push_sign.setText(_translate("Dialog", "SIGNUP")) def loginCheck(self): usernam=self.user_lineEdit.text() passwor=self.pass_lineEdit.text() conn = sqlite3.connect('login.db') crsr = conn.cursor() crsr.execute("SELECT * FROM users where username = '%s' and password= '%s' " %(usernam,passwor)) result = crsr.fetchall() if (len(result)>0): print("User found") Dialog.close() self.welcomeWindow =QtWidgets.QMainWindow() self.ui=welcomeClass() self.ui.setupUi(self.welcomeWindow) self.welcomeWindow.show() else: print("Unauthorized Login Attempt") self.showMessageBox('Warning','Wrong credentials entered') conn.close() def signCheck(self): print("Sign clicked") Dialog.close() self.signupMe =QtWidgets.QDialog() self.ui=Ui_Dialog2() self.ui.setupUi(self.signupMe) self.signupMe.show() if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Dialog = QtWidgets.QDialog() ui = Ui_Dialog() ui.setupUi(Dialog) Dialog.show() sys.exit(app.exec_())
welcome.py
from PyQt5 import QtCore, QtGui, QtWidgets class welcomeClass(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(372, 245) MainWindow.setStyleSheet("background-color: rgb(0, 255, 255);") self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(90, 70, 171, 91)) font = QtGui.QFont() font.setPointSize(23) font.setBold(True) font.setWeight(75) self.label.setFont(font) self.label.setObjectName("label") self.goBack = QtWidgets.QPushButton(self.centralwidget) self.goBack.setGeometry(QtCore.QRect(140, 200, 75, 23)) self.goBack.setObjectName("goBack") ####### self.goBack.clicked.connect(self.goBack1) ####### MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.label.setText(_translate("MainWindow", "WELCOME")) self.goBack.setText(_translate("MainWindow", "GO BACK")) def goBack1(self): from login import Ui_Dialog self.loginWindow =QtWidgets.QDialog() self.ui=Ui_Dialog() self.ui.setupUi(self.loginWindow) self.loginWindow.show() if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
我的要求的简化示例如下。
from PyQt5 import QtCore, QtGui, QtWidgets from pb2 import Ui_MainWindow2 class Ui_MainWindow1(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(633, 328) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.pb1 = QtWidgets.QPushButton(self.centralwidget) self.pb1.setGeometry(QtCore.QRect(220, 110, 191, 81)) self.pb1.setObjectName("pb1") MainWindow.setCentralWidget(self.centralwidget) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) ####### self.pb1.clicked.connect(self.signCheck1) ####### self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.pb1.setText(_translate("MainWindow", "PushButton")) def signCheck1(self): print("Sign 1 clicked") MainWindow.close() self.pb2 =QtWidgets.QMainWindow() self.ui=Ui_MainWindow2() self.ui.setupUi(self.pb2) self.pb2.show() if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow1() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow2(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(507, 328) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.pb2 = QtWidgets.QPushButton(self.centralwidget) self.pb2.setGeometry(QtCore.QRect(170, 120, 211, 91)) self.pb2.setObjectName("pb2") MainWindow.setCentralWidget(self.centralwidget) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) ####### self.pb2.clicked.connect(self.signCheck2) ####### self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.pb2.setText(_translate("MainWindow", "PushButton")) def signCheck2(self): print("Sign 2 clicked") from pb1 import Ui_MainWindow1 MainWindow.close() self.pb1 =QtWidgets.QMainWindow() self.ui=Ui_MainWindow1() self.ui.setupUi(self.pb1) self.pb1.show() if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow2() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
基本上我有2个QMainWindows。
UI中的QPushButton-1将关闭UI-1并打开UI-2
UI中的QPushButton-2将关闭UI-2,然后再次打开UI-1
对不起,我错过了signup.py 请在下面找到它。 任何帮助将不胜感激。
signup.py
from PyQt5 import QtCore, QtGui, QtWidgets import sqlite3 class Ui_Dialog2 (object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(485, 300) self.label = QtWidgets.QLabel(Dialog) self.label.setGeometry(QtCore.QRect(60, 40, 91, 31)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(Dialog) self.label_2.setGeometry(QtCore.QRect(60, 90, 71, 21)) self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(Dialog) self.label_3.setGeometry(QtCore.QRect(60, 130, 71, 21)) self.label_3.setObjectName("label_3") self.push_sign = QtWidgets.QPushButton(Dialog) self.push_sign.setGeometry(QtCore.QRect(250, 220, 131, 41)) self.push_sign.setObjectName("push_sign") self.push_sign.clicked.connect(self.signUp) self.lineEdit = QtWidgets.QLineEdit(Dialog) self.lineEdit.setGeometry(QtCore.QRect(150, 50, 113, 20)) self.lineEdit.setObjectName("lineEdit") self.lineEdit_2 = QtWidgets.QLineEdit(Dialog) self.lineEdit_2.setGeometry(QtCore.QRect(150, 90, 113, 20)) self.lineEdit_2.setObjectName("lineEdit_2") self.lineEdit_3 = QtWidgets.QLineEdit(Dialog) self.lineEdit_3.setGeometry(QtCore.QRect(150, 130, 311, 20)) self.lineEdit_3.setObjectName("lineEdit_3") self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "SIGN UP PAGE")) self.label.setText(_translate("Dialog", "USERNAME")) self.label_2.setText(_translate("Dialog", "PASSWORD")) self.label_3.setText(_translate("Dialog", "Email Address")) self.push_sign.setText(_translate("Dialog", "SIGN UP")) def signUp(self): usernam=self.lineEdit.text() passwor=self.lineEdit_2.text() email =self.lineEdit_3.text() conn = sqlite3.connect('login.db') print ("Opened database successfully") conn.execute("INSERT INTO users VALUES (?,?,?)" ,(usernam,email,passwor)); conn.commit() conn.close() if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Dialog = QtWidgets.QDialog() ui = Ui_Dialog() ui.setupUi(Dialog) Dialog.show() sys.exit(app.exec_())