Skip to content

Commit 28337d5

Browse files
committed
Updated Section-04 Database Handling
1 parent fc7fe5f commit 28337d5

File tree

2 files changed

+212
-0
lines changed

2 files changed

+212
-0
lines changed

Section-04/SimpleLogin.py

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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())

Section-04/SimpleLogin.ui

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>Form</class>
4+
<widget class="QWidget" name="Form">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>400</width>
10+
<height>300</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Form</string>
15+
</property>
16+
<layout class="QVBoxLayout" name="verticalLayout">
17+
<item>
18+
<widget class="QLabel" name="label_3">
19+
<property name="font">
20+
<font>
21+
<pointsize>12</pointsize>
22+
<bold>true</bold>
23+
</font>
24+
</property>
25+
<property name="text">
26+
<string>Login with MySQL &amp; PyQt6</string>
27+
</property>
28+
</widget>
29+
</item>
30+
<item>
31+
<layout class="QHBoxLayout" name="horizontalLayout">
32+
<item>
33+
<widget class="QLabel" name="label">
34+
<property name="font">
35+
<font>
36+
<bold>true</bold>
37+
</font>
38+
</property>
39+
<property name="text">
40+
<string>Username:</string>
41+
</property>
42+
</widget>
43+
</item>
44+
<item>
45+
<widget class="QLineEdit" name="lineEdit_username"/>
46+
</item>
47+
</layout>
48+
</item>
49+
<item>
50+
<layout class="QHBoxLayout" name="horizontalLayout_2">
51+
<item>
52+
<widget class="QLabel" name="label_2">
53+
<property name="font">
54+
<font>
55+
<bold>true</bold>
56+
</font>
57+
</property>
58+
<property name="text">
59+
<string>Password: </string>
60+
</property>
61+
</widget>
62+
</item>
63+
<item>
64+
<widget class="QLineEdit" name="lineEdit_password"/>
65+
</item>
66+
</layout>
67+
</item>
68+
<item>
69+
<widget class="QPushButton" name="pushButton_login">
70+
<property name="text">
71+
<string>Login</string>
72+
</property>
73+
</widget>
74+
</item>
75+
<item>
76+
<widget class="QLabel" name="label_result">
77+
<property name="text">
78+
<string/>
79+
</property>
80+
</widget>
81+
</item>
82+
</layout>
83+
</widget>
84+
<resources/>
85+
<connections/>
86+
</ui>

0 commit comments

Comments
 (0)