|
| 1 | +# Form implementation generated from reading ui file 'SimpleLogin.ui' |
| 2 | +# |
| 3 | +# Created by: PyQt6 UI code generator 6.4.2 |
| 4 | +# |
| 5 | +# WARNING: Any manual changes made to this file will be lost when pyuic6 is |
| 6 | +# run again. Do not edit this file unless you know what you are doing. |
| 7 | + |
| 8 | + |
| 9 | +from PyQt6 import QtCore, QtGui, QtWidgets |
| 10 | +from PyQt6.QtWidgets import QDialog |
| 11 | +from PyQt6.QtGui import QIcon |
| 12 | +import mysql.connector as mc |
| 13 | + |
| 14 | + |
| 15 | +class Ui_Form(object): |
| 16 | + def setupUi(self, Form): |
| 17 | + Form.setObjectName("Form") |
| 18 | + Form.resize(400, 300) |
| 19 | + self.verticalLayout = QtWidgets.QVBoxLayout(Form) |
| 20 | + self.verticalLayout.setObjectName("verticalLayout") |
| 21 | + |
| 22 | + |
| 23 | + self.label_3 = QtWidgets.QLabel(parent=Form) |
| 24 | + font = QtGui.QFont() |
| 25 | + font.setPointSize(12) |
| 26 | + font.setBold(True) |
| 27 | + self.label_3.setFont(font) |
| 28 | + self.label_3.setObjectName("label_3") |
| 29 | + # Set the text alignment to center (both horizontally and vertically) |
| 30 | + self.label_3.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter) |
| 31 | + # Set top and bottom margins for the label using style sheet |
| 32 | + self.label_3.setStyleSheet("margin-top: 0px; margin-bottom: 0px;") |
| 33 | + self.verticalLayout.addWidget(self.label_3) |
| 34 | + |
| 35 | + self.horizontalLayout = QtWidgets.QHBoxLayout() |
| 36 | + self.horizontalLayout.setObjectName("horizontalLayout") |
| 37 | + self.label = QtWidgets.QLabel(parent=Form) |
| 38 | + font = QtGui.QFont() |
| 39 | + font.setBold(True) |
| 40 | + self.label.setFont(font) |
| 41 | + self.label.setObjectName("label") |
| 42 | + self.horizontalLayout.addWidget(self.label) |
| 43 | + self.lineEdit_username = QtWidgets.QLineEdit(parent=Form) |
| 44 | + self.lineEdit_username.setObjectName("lineEdit_username") |
| 45 | + self.lineEdit_username.setStyleSheet("margin-top: 0px; margin-bottom: 10px;") |
| 46 | + self.horizontalLayout.addWidget(self.lineEdit_username) |
| 47 | + self.verticalLayout.addLayout(self.horizontalLayout) |
| 48 | + self.horizontalLayout_2 = QtWidgets.QHBoxLayout() |
| 49 | + self.horizontalLayout_2.setObjectName("horizontalLayout_2") |
| 50 | + self.label_2 = QtWidgets.QLabel(parent=Form) |
| 51 | + font = QtGui.QFont() |
| 52 | + font.setBold(True) |
| 53 | + self.label_2.setFont(font) |
| 54 | + self.label_2.setObjectName("label_2") |
| 55 | + self.horizontalLayout_2.addWidget(self.label_2) |
| 56 | + self.lineEdit_password = QtWidgets.QLineEdit(parent=Form) |
| 57 | + self.lineEdit_password.setMaxLength(8) |
| 58 | + self.lineEdit_password.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password) |
| 59 | + self.lineEdit_password.setObjectName("lineEdit_password") |
| 60 | + self.horizontalLayout_2.addWidget(self.lineEdit_password) |
| 61 | + self.verticalLayout.addLayout(self.horizontalLayout_2) |
| 62 | + self.pushButton_login = QtWidgets.QPushButton(parent=Form) |
| 63 | + self.pushButton_login.setObjectName("pushButton_login") |
| 64 | + |
| 65 | + #connect signal |
| 66 | + self.pushButton_login.clicked.connect(self.login) |
| 67 | + |
| 68 | + self.verticalLayout.addWidget(self.pushButton_login) |
| 69 | + self.label_result = QtWidgets.QLabel(parent=Form) |
| 70 | + self.label_result.setText("") |
| 71 | + self.label_result.setObjectName("label_result") |
| 72 | + self.verticalLayout.addWidget(self.label_result) |
| 73 | + |
| 74 | + self.retranslateUi(Form) |
| 75 | + QtCore.QMetaObject.connectSlotsByName(Form) |
| 76 | + |
| 77 | + def login(self): |
| 78 | + |
| 79 | + username = self.lineEdit_username.text() |
| 80 | + password = self.lineEdit_password.text() |
| 81 | + |
| 82 | + try: |
| 83 | + mydb = mc.connect( |
| 84 | + host="localhost", |
| 85 | + user="root", |
| 86 | + password="", |
| 87 | + database="pyqtdb" |
| 88 | + ) |
| 89 | + |
| 90 | + mycursor = mydb.cursor() |
| 91 | + query = "SELECT username, password from pyqtusers where username like '"+ username +"'and password like '"+ password +"'" |
| 92 | + mycursor.execute(query) |
| 93 | + |
| 94 | + result = mycursor.fetchone() |
| 95 | + |
| 96 | + if result == None: |
| 97 | + self.label_result.setText("Incorrect email or password") |
| 98 | + self.label_result.setStyleSheet("color: red") |
| 99 | + |
| 100 | + else: |
| 101 | + self.label_result.setText("You are logged in") |
| 102 | + self.label_result.setStyleSheet("color: green") |
| 103 | + mydialog = QDialog() |
| 104 | + mydialog.setModal(True) |
| 105 | + mydialog.exec() |
| 106 | + |
| 107 | + except mc.Error as e: |
| 108 | + self.label_result.setText("Error") |
| 109 | + |
| 110 | + def retranslateUi(self, Form): |
| 111 | + _translate = QtCore.QCoreApplication.translate |
| 112 | + Form.setWindowTitle(_translate("Form", "Form")) |
| 113 | + self.label_3.setText(_translate("Form", "Login with MySQL & PyQt6")) |
| 114 | + self.label.setText(_translate("Form", "Username:")) |
| 115 | + self.label_2.setText(_translate("Form", "Password: ")) |
| 116 | + self.pushButton_login.setText(_translate("Form", "Login")) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + import sys |
| 121 | + app = QtWidgets.QApplication(sys.argv) |
| 122 | + Form = QtWidgets.QWidget() |
| 123 | + ui = Ui_Form() |
| 124 | + ui.setupUi(Form) |
| 125 | + Form.show() |
| 126 | + sys.exit(app.exec()) |
0 commit comments