In this article we will see how we can create a percentile calculator using PyQt5, below is how the percentile calculator will look like
GUI implementation steps
1. Create a heading label that display the calculator name
2. Create label and line edit pair for total students, label to show what user has to enter and line edit to enter text
3. Similarly create a pair for getting rank
4. Create a push button to calculate the percentile
5. Create a label to show the calculated percentile
Back end implementation steps
1. Make the line edit to accept only the number as input
2. Add action to the push button
3. Inside the push button action get the text of the line edits
4. Check if the line edit text is empty or zero then return so that function will not execute further
5. Convert the text value into integer
6. Calculate the percentile and set this value to the label
Below is the implementation
Python3 1==
# importing librariesfromPyQt5.QtWidgetsimport*fromPyQt5importQtCore,QtGuifromPyQt5.QtGuiimport*fromPyQt5.QtCoreimport*importsysclassWindow(QMainWindow):def__init__(self):super().__init__()# setting titleself.setWindowTitle("Python ")# width of windowself.w_width=400# height of windowself.w_height=400# setting geometryself.setGeometry(100,100,self.w_width,self.w_height)# calling methodself.UiComponents()# showing all the widgetsself.show()# method for componentsdefUiComponents(self):# creating head labelhead=QLabel("Percentile Calculator",self)# setting geometry to the headhead.setGeometry(0,10,400,60)# fontfont=QFont('Times',15)font.setBold(True)font.setItalic(True)font.setUnderline(True)# setting font to the headhead.setFont(font)# setting alignment of the headhead.setAlignment(Qt.AlignCenter)# setting color effect to the headcolor=QGraphicsColorizeEffect(self)color.setColor(Qt.darkCyan)head.setGraphicsEffect(color)# creating a labelt_label=QLabel("Total Participants",self)# setting properties to the labelt_label.setAlignment(Qt.AlignCenter)t_label.setGeometry(20,100,170,40)t_label.setStyleSheet("QLabel""{""border : 2px solid black;""background : rgba(70, 70, 70, 35);""}")t_label.setFont(QFont('Times',9))# creating a QLineEdit object to get the total participantsself.total=QLineEdit(self)# accepting only number as inputonlyInt=QIntValidator()self.total.setValidator(onlyInt)# setting properties to the line editself.total.setGeometry(200,100,180,40)self.total.setAlignment(Qt.AlignCenter)self.total.setFont(QFont('Times',9))# creating a rank labelr_label=QLabel("Rank ",self)# setting properties to the labelr_label.setAlignment(Qt.AlignCenter)r_label.setGeometry(20,150,170,40)r_label.setStyleSheet("QLabel""{""border : 2px solid black;""background : rgba(70, 70, 70, 35);""}")r_label.setFont(QFont('Times',9))# creating a QLineEdit object to get the rankself.rank=QLineEdit(self)# accepting only number as inputonlyInt=QIntValidator()self.rank.setValidator(onlyInt)# setting properties to the line editself.rank.setGeometry(200,150,180,40)self.rank.setAlignment(Qt.AlignCenter)self.rank.setFont(QFont('Times',9))# creating a push buttoncalculate=QPushButton("Calculate Percentile",self)# setting geometry to the push buttoncalculate.setGeometry(125,220,150,40)# adding action to the calculate buttoncalculate.clicked.connect(self.calculate_action)# creating a label to show percentileself.result=QLabel(self)# setting properties to result labelself.result.setAlignment(Qt.AlignCenter)self.result.setGeometry(50,300,300,60)self.result.setStyleSheet("QLabel""{""border : 3px solid black;""background : white;""}")self.result.setFont(QFont('Arial',11))defcalculate_action(self):# getting total number of studentsstudents=self.total.text()# getting rankrank=self.rank.text()# if no input is given close the functioniflen(students)==0orlen(rank)==0:return# converting students into integerstudents=int(students)# converting rank into integerrank=int(rank)# if user enter total value as 0# return the functionifstudents==0orrank==0:return# variable to store the result upto 3# decimal pointsresult=round((students-rank)/students*100,3)# setting text to the result labelself.result.setText("Percentile : "+str(result))# create pyqt5 appApp=QApplication(sys.argv)# create the instance of our Windowwindow=Window()# start the appsys.exit(App.exec())