|
| 1 | +from PyQt6 import QtCore, QtGui, QtWidgets |
| 2 | +import mysql.connector as mc |
| 3 | +import re |
| 4 | + |
| 5 | + |
| 6 | +class Ui_Form(object): |
| 7 | + def setupUi(self, Form): |
| 8 | + Form.setObjectName("Form") |
| 9 | + Form.resize(500, 250) |
| 10 | + self.verticalLayout = QtWidgets.QVBoxLayout(Form) |
| 11 | + self.verticalLayout.setObjectName("verticalLayout") |
| 12 | + self.label_3 = QtWidgets.QLabel(parent=Form) |
| 13 | + font = QtGui.QFont() |
| 14 | + font.setPointSize(12) |
| 15 | + font.setBold(True) |
| 16 | + self.label_3.setFont(font) |
| 17 | + self.label_3.setObjectName("label_3") |
| 18 | + self.verticalLayout.addWidget(self.label_3) |
| 19 | + spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding) |
| 20 | + self.verticalLayout.addItem(spacerItem) |
| 21 | + self.horizontalLayout = QtWidgets.QHBoxLayout() |
| 22 | + self.horizontalLayout.setObjectName("horizontalLayout") |
| 23 | + self.label = QtWidgets.QLabel(parent=Form) |
| 24 | + font = QtGui.QFont() |
| 25 | + font.setBold(True) |
| 26 | + self.label.setFont(font) |
| 27 | + self.label.setObjectName("label") |
| 28 | + self.horizontalLayout.addWidget(self.label) |
| 29 | + self.lineEdit_username = QtWidgets.QLineEdit(parent=Form) |
| 30 | + self.lineEdit_username.setObjectName("lineEdit_username") |
| 31 | + self.horizontalLayout.addWidget(self.lineEdit_username) |
| 32 | + self.verticalLayout.addLayout(self.horizontalLayout) |
| 33 | + spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding) |
| 34 | + self.verticalLayout.addItem(spacerItem1) |
| 35 | + self.horizontalLayout_2 = QtWidgets.QHBoxLayout() |
| 36 | + self.horizontalLayout_2.setObjectName("horizontalLayout_2") |
| 37 | + self.label_2 = QtWidgets.QLabel(parent=Form) |
| 38 | + font = QtGui.QFont() |
| 39 | + font.setBold(True) |
| 40 | + self.label_2.setFont(font) |
| 41 | + self.label_2.setObjectName("label_2") |
| 42 | + self.horizontalLayout_2.addWidget(self.label_2) |
| 43 | + self.lineEdit_password = QtWidgets.QLineEdit(parent=Form) |
| 44 | + self.lineEdit_password.setMaxLength(8) |
| 45 | + self.lineEdit_password.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password) |
| 46 | + self.lineEdit_password.setObjectName("lineEdit_password") |
| 47 | + self.horizontalLayout_2.addWidget(self.lineEdit_password) |
| 48 | + self.verticalLayout.addLayout(self.horizontalLayout_2) |
| 49 | + spacerItem2 = QtWidgets.QSpacerItem(20, 20, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding) |
| 50 | + self.verticalLayout.addItem(spacerItem2) |
| 51 | + self.pushButton_insert = QtWidgets.QPushButton(parent=Form) |
| 52 | + font = QtGui.QFont() |
| 53 | + font.setPointSize(10) |
| 54 | + font.setBold(True) |
| 55 | + self.pushButton_insert.setFont(font) |
| 56 | + self.pushButton_insert.setObjectName("pushButton_insert") |
| 57 | + |
| 58 | + self.pushButton_insert.clicked.connect(self.insert_data) |
| 59 | + self.verticalLayout.addWidget(self.pushButton_insert) |
| 60 | + self.label_result = QtWidgets.QLabel(parent=Form) |
| 61 | + font = QtGui.QFont() |
| 62 | + font.setBold(True) |
| 63 | + self.label_result.setFont(font) |
| 64 | + self.label_result.setText("") |
| 65 | + self.label_result.setObjectName("label_result") |
| 66 | + self.verticalLayout.addWidget(self.label_result) |
| 67 | + |
| 68 | + self.retranslateUi(Form) |
| 69 | + QtCore.QMetaObject.connectSlotsByName(Form) |
| 70 | + |
| 71 | + def insert_data(self): |
| 72 | + try: |
| 73 | + mydb = mc.connect( |
| 74 | + host="localhost", |
| 75 | + user="root", |
| 76 | + password="", |
| 77 | + database="pyqtdb" |
| 78 | + ) |
| 79 | + |
| 80 | + mycursor = mydb.cursor() |
| 81 | + username = self.lineEdit_username.text() |
| 82 | + password = self.lineEdit_password.text() |
| 83 | + |
| 84 | + |
| 85 | + # Password length requirements: Minimum 6 characters, Maximum 6 characters |
| 86 | + if len(password) != 8: |
| 87 | + self.label_result.setText("Password length must be 8 characters.") |
| 88 | + self.label_result.setStyleSheet("color: red") |
| 89 | + return |
| 90 | + |
| 91 | + # Password strength requirements: At least 1 digit, 1 uppercase letter, and 1 special character |
| 92 | + if not (re.search(r"\d", password) and re.search(r"[A-Z]", password) and re.search(r"[!@#$%^&*()_+{}|:<>?~-]", password)): |
| 93 | + self.label_result.setText("Weak password!") |
| 94 | + self.label_result.setStyleSheet("color: red") |
| 95 | + return |
| 96 | + |
| 97 | + query = "INSERT INTO pyqtusers (username, password) VALUES (%s, %s)" |
| 98 | + value = (username, password) |
| 99 | + |
| 100 | + mycursor.execute(query, value) |
| 101 | + mydb.commit() |
| 102 | + |
| 103 | + self.label_result.setText("Data Inserted") |
| 104 | + self.label_result.setStyleSheet("color: green") |
| 105 | + |
| 106 | + except mc.Error as e: |
| 107 | + self.label_result.setText("Error Inserting Data.") |
| 108 | + self.label_result.setStyleSheet("color: red") |
| 109 | + |
| 110 | + |
| 111 | + def retranslateUi(self, Form): |
| 112 | + _translate = QtCore.QCoreApplication.translate |
| 113 | + Form.setWindowTitle(_translate("Form", "Form")) |
| 114 | + self.label_3.setText(_translate("Form", "Inserting Data to MySQL Database")) |
| 115 | + self.label.setText(_translate("Form", "Username:")) |
| 116 | + self.label_2.setText(_translate("Form", "Password: ")) |
| 117 | + self.pushButton_insert.setText(_translate("Form", "Insert Data")) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + import sys |
| 122 | + app = QtWidgets.QApplication(sys.argv) |
| 123 | + Form = QtWidgets.QWidget() |
| 124 | + ui = Ui_Form() |
| 125 | + ui.setupUi(Form) |
| 126 | + Form.show() |
| 127 | + sys.exit(app.exec()) |
0 commit comments