Rank Based Percentile GUI Calculator using PyQt5 in Python
PyQt5:
PyQt5 is one of several solutions that Python offers for creating GUI applications. Cross-platform GUI toolkit PyQt5 is a collection of Python interfaces for Qt version 5. With the capabilities and convenience of use that this library offers, one can create an interactive computer program very quickly. Front-end and Back-end make up a GUI application. In order to speed up development & allow more time to be spent on back-end tasks, PyQt5 has developed a tool called "QtDesigner" that allows one to drag and drop elements to create the front-end. Riverbank Computing is the company in charge of developing and maintaining PyQt. A most recent stable version is PyQt6. The PyQt main version's release cycle matches Qt's, as per the release history. The PyQt codebase is a complex system with both C++ and Py code at its core. It is therefore more difficult to create and download it from the sources than other Python libraries. Even more detailed installation instructions may be found in the instructions for the specific PyQt version we want to utilise. It comes with installation instructions for both the GPL and the paid versions. Before the window shows on the screen, a few essential concepts regarding the organisation of applications in the Qt world must be explained. Unless you're already familiar with event loops, you can proceed to the next part without risk. The cornerstone of all Qt programs is the QApplication class. For each application to function, there must be a single QApplication object.
Installation:
pip install pyqt5
pip install pyqt5-tools
CODE:
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Python ")
self.w_width = 400
self.w_height = 400
self.setGeometry(100, 100, self.w_width, self.w_height)
self.UiComponents()
self.show()
# method for components
def UiComponents(self):
head = QLabel("Percentile Calculator", self)
head.setGeometry(0, 10, 400, 60)
font = QFont('Times', 15)
font.setBold(True)
font.setItalic(True)
font.setUnderline(True)
head.setFont(font)
head.setAlignment(Qt.AlignCenter)
color = QGraphicsColorizeEffect(self)
color.setColor(Qt.darkCyan)
head.setGraphicsEffect(color)
t_label = QLabel("Total Participants", self)
t_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))
self.total = QLineEdit(self)
onlyInt = QIntValidator()
self.total.setValidator(onlyInt)
self.total.setGeometry(200, 100, 180, 40)
self.total.setAlignment(Qt.AlignCenter)
self.total.setFont(QFont('Times', 9))
r_label = QLabel("Rank ", self)
r_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))
self.rank = QLineEdit(self)
onlyInt = QIntValidator()
self.rank.setValidator(onlyInt)
self.rank.setGeometry(200, 150, 180, 40)
self.rank.setAlignment(Qt.AlignCenter)
self.rank.setFont(QFont('Times', 9))
calculate = QPushButton("Calculate Percentile", self)
calculate.setGeometry(125, 220, 150, 40)
calculate.clicked.connect(self.calculate_action)
self.result = QLabel(self)
self.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))
def calculate_action(self):
students = self.total.text()
rank = self.rank.text()
if len(students) == 0 or len(rank) == 0:
return
students = int(students)
rank = int(rank)
if students == 0 or rank == 0:
return
result = round((students - rank) / students * 100, 3)
self.result.setText("Percentile : " + str(result))
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
CODE EXPLANATION:
Importing the required libraries that are PyQt5.QtWidgets, PyQt5.QtCore, qtGui, sys. From the function Window and init self, we settle a title named Python for the header window to show on brief. We declare a title, include the width and height, and set a perfect geometry with Ui components. Creating another function named UiComponents by creating a head label and setting the geometry to head and font with QFont to set different types like bold, italic, and underline. After setting the font, we go for the color to color with QGraphics colorize effect and create a label called Total partitions and properties to the label. Then creating a QLineEdit object to get the total participants and accept only the number as valid input by setting properties to the line edit with setGeometry and creating a rank label by setting properties to the label. Then we create a QLineEdit object to get the rank. Accepting only the number as input and setting properties to the line edit, we create a push button with geometry and add action to the calculate button. Finally, we create the instance of our window to start the app with the library sys.
OUTPUT:
