PyQt5 - Create a User form to get information - GeeksforGeeks

03 Jan, 2022
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 QDialog2. Add window title and set its geometry3. Create a QGropBox object4. Create line edit to get the name, spin box to get the age and combo box to select the degree5. Create a createForm method that will create a form6. Inside the create form method, create a form layout and add rows7. 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 state9. Add action to the button for acceptance and for rejection10. 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
Python3
# importing librariesfrom PyQt5.QtWidgets import * import sys  # creating a class# that inherits the QDialog classclass Window(QDialog):      # constructor    def __init__(self):        super(Window, self).__init__()          # setting window title        self.setWindowTitle("Python")          # 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        self.createForm()          # creating a dialog button for ok and cancel        self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)          # adding action when form is accepted        self.buttonBox.accepted.connect(self.getInfo)          # adding action when form is rejected        self.buttonBox.rejected.connect(self.reject)          # creating a vertical layout        mainLayout = QVBoxLayout()          # adding form group box to the layout        mainLayout.addWidget(self.formGroupBox)          # adding button box to the layout        mainLayout.addWidget(self.buttonBox)          # setting lay out        self.setLayout(mainLayout)      # 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        self.close()      # 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        self.formGroupBox.setLayout(layout)    # main methodif __name__ == '__main__':      # create pyqt5 app    app = QApplication(sys.argv)      # create the instance of our Window    window = Window()      # showing the window    window.show()      # start the app    sys.exit(app.exec())
Output :
Person Name : Geek
Degree : BTech
Age : 20

akshaysingh98088
PyQt-exercise
Python-gui
Python-PyQt
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists
Convert integer to string in Python