In this PyQt5 Tutorial we want to learn creating of Simple Login with MySQL in PyQt5, before this we have learned how you can work with mysql database and pyqt5, we have learned about mysql database connection, inserting data and selecting data from mysql database in pyqt5. you can check the articles in the below links.
- How to Create MySQL Database in PyQt5
- How to Insert Data to Mysql Database in PyQt5
- Selecting Data from Mysql Database in QTableWidget
As i have already said we want to use MySQL Connector, you need to install this library.
1 |
pip install mysql-connector-python |
Make sure that you have already installed Wamp Server, and you have the database and table in your Wamp Server, you need to read the previous articles that i have already added the links at the top.
Open your Qt Designer, you can just write pyqt5designer in your terminal, after opening the Qt Designer you need to create Widget window. now we add widgets in Qt Designer.
- Add QHBoxLayout in QHBoxLayout add a QLabel and QLinEdit
- Add another QHBoxLayout, you need to also add a QLabel and QLineEdit in this layout
- Add a QVBoxLayout, in this layout add a QLabel with a QPushButton
- At the end click on the main window and select layout vertically for all widgets
- Also you need to add a vertical spacer between the lineedits and button.
This is the design that we want.
After completing the design you need to save your design, the extension will be .ui file. now it is time to convert our .ui file in to .py file. there are two ways that you can do, first way is loading the ui file, the second way is converting the .ui file to .py file, we want to use the second way. for converting of the ui file to python file we need to use pyuic5 module, this module is located in the Scripts folder of your python installation, you need to copy your ui file in the Scripts folder of your Python installation. after that open the terminal in the Scripts folder, and run this command.
1 |
pyuic5 signin.ui -o signin.py -x |
And this is the converted file, also we have added a method for login.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'signin.ui' # # Created by: PyQt5 UI code generator 5.15.1 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets import mysql.connector as mc from PyQt5.QtWidgets import QDialog class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(453, 352) self.verticalLayout_2 = QtWidgets.QVBoxLayout(Form) self.verticalLayout_2.setObjectName("verticalLayout_2") self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setObjectName("horizontalLayout") self.label = QtWidgets.QLabel(Form) self.label.setObjectName("label") self.horizontalLayout.addWidget(self.label) self.lineEditEmail = QtWidgets.QLineEdit(Form) self.lineEditEmail.setObjectName("lineEditEmail") self.horizontalLayout.addWidget(self.lineEditEmail) self.verticalLayout_2.addLayout(self.horizontalLayout) self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.label_2 = QtWidgets.QLabel(Form) self.label_2.setObjectName("label_2") self.horizontalLayout_2.addWidget(self.label_2) self.lineEditPassword = QtWidgets.QLineEdit(Form) self.lineEditPassword.setEchoMode(QtWidgets.QLineEdit.Password) self.lineEditPassword.setObjectName("lineEditPassword") self.horizontalLayout_2.addWidget(self.lineEditPassword) self.verticalLayout_2.addLayout(self.horizontalLayout_2) spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.verticalLayout_2.addItem(spacerItem) self.verticalLayout = QtWidgets.QVBoxLayout() self.verticalLayout.setObjectName("verticalLayout") self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setObjectName("pushButton") #clicked signal of the button connected to login method self.pushButton.clicked.connect(self.login) self.verticalLayout.addWidget(self.pushButton) self.labelResult = QtWidgets.QLabel(Form) font = QtGui.QFont() font.setPointSize(14) font.setBold(True) font.setWeight(75) self.labelResult.setFont(font) self.labelResult.setText("") self.labelResult.setObjectName("labelResult") self.verticalLayout.addWidget(self.labelResult) self.verticalLayout_2.addLayout(self.verticalLayout) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) #method for login, we have connected this with the clicked signal of button def login(self): try: email = self.lineEditEmail.text() password = self.lineEditPassword.text() mydb = mc.connect( host="localhost", user="root", password="", database="pythondb" ) mycursor = mydb.cursor() mycursor.execute("SELECT email,password from users where email like '"+email + "'and password like '"+password+"'") result = mycursor.fetchone() if result == None: self.labelResult.setText("Incorrect Email & Password") else: self.labelResult.setText("You are logged in") mydialog = QDialog() mydialog.setModal(True) mydialog.exec() except mc.Error as e: self.labelResult.setText("Error") def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label.setText(_translate("Form", "Email:")) self.label_2.setText(_translate("Form", "Password:")) self.pushButton.setText(_translate("Form", "Login")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec_()) |
We have added one method in our file and that is for login, first we want to get the email and password from the user and after we compare that with the registered email an password of our database. in the successful login we want to open the second dialog.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
def login(self): try: email = self.lineEditEmail.text() password = self.lineEditPassword.text() mydb = mc.connect( host="localhost", user="root", password="", database="mydb" ) mycursor = mydb.cursor() mycursor.execute("SELECT email,password from users where email like '"+email + "'and password like '"+password+"'") result = mycursor.fetchone() if result == None: self.labelResult.setText("Incorrect Email & Password") else: self.labelResult.setText("You are logged in") mydialog = QDialog() mydialog.setModal(True) mydialog.exec() except mc.Error as e: self.labelResult.setText("Error") |
Run the complete code give correct username and password.
Subscribe and Get Free Video Courses & Articles in your Email