Skip to content

Commit 0be2fac

Browse files
committed
Update Section-04,05
1 parent 160c846 commit 0be2fac

25 files changed

+1749
-28
lines changed
2.42 KB
Binary file not shown.

Democehk/main.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from PyQt6 import QtWidgets
2+
from ui_design import Ui_LoginDialog, Ui_HomeDialog
3+
4+
class LoginDialog(QtWidgets.QDialog, Ui_LoginDialog):
5+
def __init__(self):
6+
super().__init__()
7+
self.setupUi(self)
8+
self.pushButton_login.clicked.connect(self.login)
9+
10+
def login(self):
11+
# Perform login authentication here
12+
username = self.lineEdit_username.text()
13+
password = self.lineEdit_password.text()
14+
15+
# For simplicity, assume successful login for "admin" with password "admin123"
16+
if username == "admin" and password == "admin123":
17+
self.switch_to_home_page()
18+
19+
def switch_to_home_page(self):
20+
self.home_page = HomeDialog()
21+
self.home_page.show()
22+
self.close() # Automatically close the Login dialog
23+
24+
class HomeDialog(QtWidgets.QDialog, Ui_HomeDialog):
25+
def __init__(self):
26+
super().__init__()
27+
self.setupUi(self)
28+
self.pushButton_logout.clicked.connect(self.logout)
29+
30+
def logout(self):
31+
self.close()
32+
self.login_page = LoginDialog()
33+
self.login_page.show()
34+
35+
if __name__ == "__main__":
36+
import sys
37+
app = QtWidgets.QApplication(sys.argv)
38+
39+
login_page = LoginDialog()
40+
login_page.show()
41+
42+
sys.exit(app.exec())

Democehk/ui_design.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from PyQt6 import QtCore, QtGui, QtWidgets
2+
3+
class Ui_LoginDialog(object):
4+
def setupUi(self, LoginDialog):
5+
LoginDialog.setObjectName("LoginDialog")
6+
LoginDialog.resize(300, 200)
7+
self.verticalLayout = QtWidgets.QVBoxLayout(LoginDialog)
8+
self.verticalLayout.setObjectName("verticalLayout")
9+
self.label = QtWidgets.QLabel(LoginDialog)
10+
font = QtGui.QFont()
11+
font.setPointSize(16)
12+
font.setBold(True)
13+
self.label.setFont(font)
14+
self.label.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
15+
self.label.setObjectName("label")
16+
self.verticalLayout.addWidget(self.label)
17+
self.lineEdit_username = QtWidgets.QLineEdit(LoginDialog)
18+
self.lineEdit_username.setObjectName("lineEdit_username")
19+
self.verticalLayout.addWidget(self.lineEdit_username)
20+
self.lineEdit_password = QtWidgets.QLineEdit(LoginDialog)
21+
self.lineEdit_password.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password)
22+
self.lineEdit_password.setObjectName("lineEdit_password")
23+
self.verticalLayout.addWidget(self.lineEdit_password)
24+
self.pushButton_login = QtWidgets.QPushButton(LoginDialog)
25+
self.pushButton_login.setObjectName("pushButton_login")
26+
self.verticalLayout.addWidget(self.pushButton_login)
27+
28+
self.retranslateUi(LoginDialog)
29+
QtCore.QMetaObject.connectSlotsByName(LoginDialog)
30+
31+
def retranslateUi(self, LoginDialog):
32+
_translate = QtCore.QCoreApplication.translate
33+
LoginDialog.setWindowTitle(_translate("LoginDialog", "Login"))
34+
self.label.setText(_translate("LoginDialog", "Login"))
35+
self.pushButton_login.setText(_translate("LoginDialog", "Login"))
36+
37+
38+
class Ui_HomeDialog(object):
39+
def setupUi(self, HomeDialog):
40+
HomeDialog.setObjectName("HomeDialog")
41+
HomeDialog.resize(300, 200)
42+
self.verticalLayout = QtWidgets.QVBoxLayout(HomeDialog)
43+
self.verticalLayout.setObjectName("verticalLayout")
44+
self.label = QtWidgets.QLabel(HomeDialog)
45+
font = QtGui.QFont()
46+
font.setPointSize(16)
47+
font.setBold(True)
48+
self.label.setFont(font)
49+
self.label.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
50+
self.label.setObjectName("label")
51+
self.verticalLayout.addWidget(self.label)
52+
self.pushButton_logout = QtWidgets.QPushButton(HomeDialog)
53+
self.pushButton_logout.setObjectName("pushButton_logout")
54+
self.verticalLayout.addWidget(self.pushButton_logout)
55+
56+
self.retranslateUi(HomeDialog)
57+
QtCore.QMetaObject.connectSlotsByName(HomeDialog)
58+
59+
def retranslateUi(self, HomeDialog):
60+
_translate = QtCore.QCoreApplication.translate
61+
HomeDialog.setWindowTitle(_translate("HomeDialog", "Home"))
62+
self.label.setText(_translate("HomeDialog", "Welcome to Home"))
63+
self.pushButton_logout.setText(_translate("HomeDialog", "Logout"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Form implementation generated from reading ui file 'DbConnection.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+
import mysql.connector as mc
11+
12+
13+
class Ui_Form(object):
14+
def setupUi(self, Form):
15+
Form.setObjectName("Form")
16+
Form.resize(600, 300)
17+
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
18+
self.verticalLayout.setObjectName("verticalLayout")
19+
self.horizontalLayout = QtWidgets.QHBoxLayout()
20+
self.horizontalLayout.setObjectName("horizontalLayout")
21+
self.label = QtWidgets.QLabel(parent=Form)
22+
self.label.setObjectName("label")
23+
self.horizontalLayout.addWidget(self.label)
24+
self.lineEdit = QtWidgets.QLineEdit(parent=Form)
25+
self.lineEdit.setObjectName("lineEdit")
26+
self.horizontalLayout.addWidget(self.lineEdit)
27+
self.verticalLayout.addLayout(self.horizontalLayout)
28+
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
29+
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
30+
self.pushButton_createdb = QtWidgets.QPushButton(parent=Form)
31+
self.pushButton_createdb.setObjectName("pushButton_createdb")
32+
33+
#signal connection
34+
self.pushButton_createdb.clicked.connect(self.create_database)
35+
self.horizontalLayout_2.addWidget(self.pushButton_createdb)
36+
self.pushButton_dbcon = QtWidgets.QPushButton(parent=Form)
37+
self.pushButton_dbcon.setObjectName("pushButton_dbcon")
38+
39+
#signal connection
40+
self.pushButton_dbcon.clicked.connect(self.db_connect)
41+
self.horizontalLayout_2.addWidget(self.pushButton_dbcon)
42+
self.verticalLayout.addLayout(self.horizontalLayout_2)
43+
self.label_result = QtWidgets.QLabel(parent=Form)
44+
self.label_result.setText("")
45+
self.label_result.setObjectName("label_result")
46+
self.verticalLayout.addWidget(self.label_result)
47+
48+
self.retranslateUi(Form)
49+
QtCore.QMetaObject.connectSlotsByName(Form)
50+
51+
def create_database(self):
52+
try:
53+
mydb = mc.connect(
54+
host ="localhost",
55+
user ="root",
56+
password =""
57+
)
58+
59+
cursor = mydb.cursor()
60+
dbname = self.lineEdit.text()
61+
62+
cursor.execute("CREATE DATABASE {} ".format(dbname))
63+
self.label_result.setText("Database {} Created ".format(dbname))
64+
except mc.Error as e:
65+
self.label_result.setText("Database Creation failed")
66+
67+
def db_connect(self):
68+
try:
69+
mydb = mc.connect(
70+
host ="localhost",
71+
user ="root",
72+
password ="",
73+
database="pyqtdb"
74+
)
75+
76+
self.label_result.setText("There is a connection")
77+
except mc.Error as e:
78+
self.label_result.setText("Error in connection")
79+
80+
def retranslateUi(self, Form):
81+
_translate = QtCore.QCoreApplication.translate
82+
Form.setWindowTitle(_translate("Form", "Form"))
83+
self.label.setText(_translate("Form", "Database Name"))
84+
self.pushButton_createdb.setText(_translate("Form", "Create Database"))
85+
self.pushButton_dbcon.setText(_translate("Form", "Database Connection"))
86+
87+
88+
if __name__ == "__main__":
89+
import sys
90+
app = QtWidgets.QApplication(sys.argv)
91+
Form = QtWidgets.QWidget()
92+
ui = Ui_Form()
93+
ui.setupUi(Form)
94+
Form.show()
95+
sys.exit(app.exec())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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

Comments
 (0)