Skip to content
Related Articles

Related Articles

PyQt5 – Create a User form to get information
  • Last Updated : 01 Jun, 2020

In this article we will see how we can create a user form in PyQt5. A user form is basically a dialog box that makes a user data entry more controllable and easier to use for the user. Sometimes while creating a large user interface application there is need of creating user form in order to get essential information.

Implementation steps :

1. Create a window class that inherits QDialog
2. Add window title and set its geometry
3. Create a QGropBox object
4. Create line edit to get the name, spin box to get the age and combo box to select the degree
5. Create a createForm method that will create a form
6. Inside the create form method, create a form layout and add rows
7. Each row should be has a label and the input method example name label and line edit for input, similarly label for degree and age and combo box and spin box for input.
8. Create a QDialogButtonBox for ok and cancel state
9. Add action to the button for acceptance and for rejection
10. If ok button is pressed just print all the information and if the cancel button is pressed window will get closed

Below is the implementation

# importing libraries
from PyQt5.QtWidgets import * 
import sys
# creating a class
# that inherits the QDialog class
class Window(QDialog):
    # constructor
    def __init__(self):
        super(Window, self).__init__()
        # setting window title
        # setting geometry to the window
        self.setGeometry(100, 100, 300, 400)
        # creating a group box
        self.formGroupBox = QGroupBox("Form 1")
        # creating spin box to select age
        self.ageSpinBar = QSpinBox()
        # creating combo box to select degree
        self.degreeComboBox = QComboBox()
        # adding items to the combo box
        self.degreeComboBox.addItems(["BTech", "MTech", "PhD"])
        # creating a line edit
        self.nameLineEdit = QLineEdit()
        # calling the method that create the form
        # creating a dialog button for ok and cancel
        self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        # adding action when form is accepted
        # addding action when form is rejected
        # creating a vertical layout
        mainLayout = QVBoxLayout()
        # adding form group box to the layout
        # adding button box to the layout
        # setting lay out
    # get info method called when form is accepted
    def getInfo(self):
        # printing the form information
        print("Person Name : {0}".format(self.nameLineEdit.text()))
        print("Degree : {0}".format(self.degreeComboBox.currentText()))
        print("Age : {0}".format(self.ageSpinBar.text()))
        # closing the window
    # creat form method
    def createForm(self):
        # creating a form layout
        layout = QFormLayout()
        # adding rows
        # for name and adding input text
        layout.addRow(QLabel("Name"), self.nameLineEdit)
        # for degree and adding combo box
        layout.addRow(QLabel("Degree"), self.degreeComboBox)
        # for age and adding spin box
        layout.addRow(QLabel("Age"), self.ageSpinBar)
        # setting layout
# main method
if __name__ == '__main__':
    # create pyqt5 app
    app = QApplication(sys.argv)
    # create the instance of our Window
    window = Window()
    # showing the window
    # start the app

Output :

Person Name : Geek
Degree : BTech
Age : 20

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :