Create a Simple Login Form using PyQt5 in Python

In this module, we are going to discuss the creation of a simple login form using PyQt5 in Python. PyQt5 is a graphical user interface(GUI) tool kit that allows us to create various GUI elements like Buttons, login pages, dialog boxes, etc.

Creating a Login Form Using PyQt5

The following code will create a simple login form as follows

import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QLabel, QLineEdit, QGridLayout)
class Loginpage(QWidget):
  def __init__(self):
    super().__init__()
    self.setWindowTitle('Login')
    self.resize(350,200)
    layout = QGridLayout()
    label1 = QLabel('<font size="8"> UserId </font>')
    self.user_obj = QLineEdit()
    layout.addWidget(label1, 0, 0)
    layout.addWidget(self.user_obj, 0, 1)
    label2 = QLabel('<font size="8"> Password </font>')
    self.user_pwd = QLineEdit()
    layout.addWidget(label2, 1, 0)
    layout.addWidget(self.user_pwd, 1, 1)
    button_login = QPushButton('Login')
    layout.addWidget(button_login, 2, 0, 2, 2)
    self.setLayout(layout)
app = QApplication(sys.argv)
form = Loginpage()
form.show()
sys.exit(app.exec_())

Output:

Create a Simple Login Form using PyQt5 in Python

We get a simple login page that asks for user id and password

from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QLabel, QLineEdit, QGridLayout)

To get the login page we require all the mentioned submodules which are present in PyQt5.QtWidgets Module.

app = QApplication(sys.argv)

This function helps us to provide an application layout on which we perform our actions it takes argument values as input argv.

form = Loginpage()

We created an object for login page class that has inherited QWidget as it’s base class.so that it can use all elements present in the QWidget class.

self.setWindowTitle('Login')
self.resize(350,200)

The above function helps us to set a title for our login page and resize(width, height)helps us to set size for our application which is of (350,200) in our code.

layout = QGridLayout()

It helps us to take the layout of the application in the form of rows and columns.

label1 = QLabel('<font size="8"> UserId </font>')
self.user_obj = QLineEdit()
layout.addWidget(label1, 0, 0)
layout.addWidget(self.user_obj, 0, 1)

Here QLabel is used to create a Label for box-like User-Id we can alter the font with the size parameter.

  • QLineEdit(): it helps us to make the constraint editable i.e giving input.
  • addWidget(): it adds the defined constraints to the layout at the position of rows and columns. Here, label1 at zero row and zero column similarly user_obj at zero row and first column.
button_login = QPushButton('Login')
layout.addWidget(button_login, 2, 0, 2, 2)

Int the above code QPushButton() creates a button. The button becomes user interactive.

The same process is repeated for the password parameter. Now after completing the required action we set layout with our constraints using setLayout() which takes the object as a parameter on which we perform our actions.

form.show()

It will help us to display our required login page without this function we will not get any output.

Also read: Displaying an image using PyQt5 in Python

Leave a Reply

Your email address will not be published. Required fields are marked *