In this article we will see how we can create a ratio calculator using PyQt5. Ratio is a contrast, which exists between two particular numbers, is defined as ratio. This ratio calculator is developed to compute this contrast and find out the relationship between these numbers. Below is how the calculator will look 
pip install PyQt5
Concept : User has to select a ratio then enter another number for which calculator will find the corresponding ratio value, below is the formula used
A / B = C / X
Here A and B are the selected ratio and C is the entered number for which we have to find corresponding value of the ratio
GUI Implementation Steps : 1. Create a heading label that display the calculator name 2. Create three spin box for getting ‘A’, ‘B’ and ‘C’ values 3. Create labels between spin boxes for ratio and equal to symbol and label for showing ‘X’ value 4. Create push button for calculate the ratio value 5. Create a label to show the calculated value Back-End Implementation : 1. Set range to each of the spin box with minimum value equal to 1 so that user can’t enter 0 as input 2. Set various properties like alignment, geometry to each of the widget in the window 3. Add color effect to the push button 4. Add action to the push button when it get clicked 5. Inside the push button action calculate the value of ‘X’ according to the values entered by user 6. Remove decimal values if it is a whole number 7. Show the output with the help of result label
Below is the implementationÂ
Python3
# importing librariesfrom PyQt5.QtWidgets import * from PyQt5 import QtCore, QtGuifrom PyQt5.QtGui import * from PyQt5.QtCore import * import sysÂ
Â
class Window(QMainWindow):Â
    def __init__(self):        super().__init__()Â
        # setting title        self.setWindowTitle("Python ")Â
        # width of window        self.w_width = 550Â
        # height of window        self.w_height = 300Â
        # setting geometry        self.setGeometry(100, 100, self.w_width, self.w_height)Â
        # calling method        self.UiComponents()Â
        # showing all the widgets        self.show()Â
    # method for components    def UiComponents(self):Â
        # creating head label        head = QLabel("Ratio Calculator", self)Â
        head.setWordWrap(True)Â
        # setting geometry to the head        head.setGeometry(0, 10, 550, 60)Â
        # font        font = QFont('Times', 15)        font.setBold(True)        font.setItalic(True)        font.setUnderline(True)Â
        # setting font to the head        head.setFont(font)Â
        # setting alignment of the head        head.setAlignment(Qt.AlignCenter)Â
        # setting color effect to the head        color = QGraphicsColorizeEffect(self)        color.setColor(Qt.darkCyan)        head.setGraphicsEffect(color)Â
        # creating a spin box        self.a_spin = QSpinBox(self)Â
        # setting geometry        self.a_spin.setGeometry(50, 90, 90, 35)Â
        # setting alignment        self.a_spin.setAlignment(Qt.AlignCenter)Â
        # setting range        self.a_spin.setRange(1, 999999)Â
        # creating a label        l1 = QLabel(" : ", self)Â
        # setting geometry        l1.setGeometry(140, 90, 30, 35)Â
        # setting alignment        l1.setAlignment(Qt.AlignCenter)Â
        # creating a spin box        self.b_spin = QSpinBox(self)Â
        # setting geometry        self.b_spin.setGeometry(170, 90, 90, 35)Â
        # setting alignment        self.b_spin.setAlignment(Qt.AlignCenter)Â
        # setting range        self.b_spin.setRange(1, 999999)Â
        # creating a label        l2 = QLabel(" = ", self)Â
        # setting geometry        l2.setGeometry(260, 90, 30, 35)Â
        # setting alignment        l2.setAlignment(Qt.AlignCenter)Â
        # creating a spin box        self.c_spin = QSpinBox(self)Â
        # setting geometry        self.c_spin.setGeometry(290, 90, 90, 35)Â
        # setting alignment        self.c_spin.setAlignment(Qt.AlignCenter)Â
        # setting range        self.c_spin.setRange(1, 999999)Â
        # creating a label        l3 = QLabel(" : ", self)Â
        # setting geometry        l3.setGeometry(380, 90, 20, 35)Â
        # setting alignment        l3.setAlignment(Qt.AlignCenter)Â
        # creating a label        lx = QLabel("X", self)Â
        # setting geometry        lx.setGeometry(410, 90, 90, 35)Â
        # setting alignment        lx.setAlignment(Qt.AlignCenter)Â
        # setting style sheet        lx.setStyleSheet("QLabel"                         "{"                         "border : 1px solid black;"                         "background-color : white;"                         "font-size : 15px;"                         "}")Â
        # creating a push button        calculate = QPushButton("Calculate", self)Â
        # setting geometry to the push button        calculate.setGeometry(175, 150, 200, 40)Â
        # adding action to the button        calculate.clicked.connect(self.calculate)Â
        # adding color effect to the push button        color = QGraphicsColorizeEffect()        color.setColor(Qt.darkGreen)        calculate.setGraphicsEffect(color)Â
        # creating a label to show result        self.result = QLabel(self)Â
        # setting properties to result label        self.result.setAlignment(Qt.AlignCenter)Â
        # setting geometry        self.result.setGeometry(125, 210, 300, 60)Â
        # making it multi line        self.result.setWordWrap(True)Â
        # setting stylesheet        # adding border and background        self.result.setStyleSheet("QLabel"                                  "{"                                  "border : 3px solid black;"                                  "background : white;"                                  "}")Â
        # setting font        self.result.setFont(QFont('Arial', 11))Â
Â
    def calculate(self):Â
        # getting spin box 'a' value        a = self.a_spin.value()Â
        # getting spin box 'b' value        b = self.b_spin.value()Â
        # getting spin box 'c value        c = self.c_spin.value()Â
        # calculating 'x' value        x = (c * b) / aÂ
        # removing decimal if decimal is zero        if x % 1 == 0:            x = int(x)Â
Â
        # setting text to the result label        self.result.setText(str(a) + " : " + str(b) + " = "                            + str(c) + " : " + str(x))Â
Â
Â
Â
Â
# create pyqt5 appApp = QApplication(sys.argv)Â
# create the instance of our Windowwindow = Window()Â
# start the appsys.exit(App.exec()) |
Output :
