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
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()
# create 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 method if __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