Introduction
In this PyQt5 blog of Python, I will show you a simple and attractive login form.
Requirement
Python IDLE 3.7
Before moving on here, you should read my previous articles that will be very useful to you:
Source code
- from PyQt5 import QtCore, QtGui, QtWidgets
-
- class Ui_Dialog(object):
- def setupUi(self, Dialog):
- Dialog.setObjectName("Dialog")
- Dialog.resize(812, 632)
- Dialog.setStyleSheet("background-color: rgb(0, 170, 255);")
- self.frame = QtWidgets.QFrame(Dialog)
- self.frame.setGeometry(QtCore.QRect(90, 80, 631, 461))
- self.frame.setStyleSheet("background-color: rgb(255, 255, 255);")
- self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
- self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
- self.frame.setObjectName("frame")
- self.label = QtWidgets.QLabel(self.frame)
- self.label.setGeometry(QtCore.QRect(230, 80, 171, 51))
- font = QtGui.QFont()
- font.setPointSize(16)
- self.label.setFont(font)
- self.label.setStyleSheet("color: rgb(255, 0, 0);")
- self.label.setObjectName("label")
- self.label_2 = QtWidgets.QLabel(self.frame)
- self.label_2.setGeometry(QtCore.QRect(90, 190, 121, 31))
- font = QtGui.QFont()
- font.setPointSize(12)
- self.label_2.setFont(font)
- self.label_2.setObjectName("label_2")
- self.label_3 = QtWidgets.QLabel(self.frame)
- self.label_3.setGeometry(QtCore.QRect(90, 260, 121, 21))
- font = QtGui.QFont()
- font.setPointSize(12)
- self.label_3.setFont(font)
- self.label_3.setObjectName("label_3")
- self.lineEdit = QtWidgets.QLineEdit(self.frame)
- self.lineEdit.setGeometry(QtCore.QRect(260, 190, 231, 31))
- self.lineEdit.setStyleSheet("background-color: rgb(209, 207, 255);")
- self.lineEdit.setObjectName("lineEdit")
- self.lineEdit_2 = QtWidgets.QLineEdit(self.frame)
- self.lineEdit_2.setGeometry(QtCore.QRect(260, 260, 231, 31))
- self.lineEdit_2.setStyleSheet("background-color:#d1cfff;")
- self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
- self.lineEdit_2.setObjectName("lineEdit_2")
- self.pushButton = QtWidgets.QPushButton(self.frame)
- self.pushButton.setGeometry(QtCore.QRect(350, 360, 161, 41))
- font = QtGui.QFont()
- font.setPointSize(14)
- self.pushButton.setFont(font)
- self.pushButton.setStyleSheet("background-color: rgb(0, 170, 0);")
- self.pushButton.setObjectName("pushButton")
- self.pushButton_2 = QtWidgets.QPushButton(self.frame)
- self.pushButton_2.setGeometry(QtCore.QRect(220, 360, 101, 41))
- self.pushButton_2.setStyleSheet("background-color:#ffff7f;")
- self.pushButton_2.setObjectName("pushButton_2")
-
- self.retranslateUi(Dialog)
- QtCore.QMetaObject.connectSlotsByName(Dialog)
-
- def retranslateUi(self, Dialog):
- _translate = QtCore.QCoreApplication.translate
- Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
- self.label.setText(_translate("Dialog", "Log in Form"))
- self.label_2.setText(_translate("Dialog", "User Name"))
- self.label_3.setText(_translate("Dialog", "Password"))
- self.pushButton.setText(_translate("Dialog", "Log in"))
- self.pushButton_2.setText(_translate("Dialog", "Sign up"))
-
-
- if __name__ == "__main__":
- import sys
- app = QtWidgets.QApplication(sys.argv)
- Dialog = QtWidgets.QDialog()
- ui = Ui_Dialog()
- ui.setupUi(Dialog)
- Dialog.show()
- sys.exit(app.exec_())
Source Description
- Here, I imported PyQt5 and their extension modules such as QtWidgets, QtGui and QtCore.
- Next, I created one Class and two functions to declare Python widgets.
- Then I created a label for Form title by using QLabel and setGeomentory methods.
- Next, I declared two line edits for getting input and two pushbuttons for posting data to Database along with their configuration like setobject name, set stylesheet, and setFont.
Output
Let’s run the above source using Python IDLE. The code runs available various Python IDE such as PyCharm, Spyder, and eclipse, etc.
- Open the Python IDLE 3.7 and click on Run >> Run module.