Creating Golden Ratio Calculator using PyQt5

In this article we will see how we can create a golden ratio calculator using PyQt5. In mathematics, two quantities are in the golden ratio if their ratio is the same as the ratio of their sum to the larger of the two quantities. The value of golden ratio is 1.61803398875. Below is how the golden ratio calculator will look like 
pip install PyQt5
Concept : Below is the formula for calculating golden ratio
A / B = (A + B) / A = golden_ratio
Here A is the larger length and B is the shorter i.e second part of the length and the value of golden ratio is 1.61803398875.
GUI Implementation Steps : 1. Create a heading label that display the calculator name 2. Create three radio buttons for first, second and sum of the lengths 3. Create three spin boxes for user to enter the specific length 4. Create push button for calculating the other values according to golden ratio 5. Create a label to show the calculated values Back-End Implementation : 1. Initially make all the spin boxes disable 2. Add same action to all the three radio button 3. Inside the radio button method which radio button is checked 4. According to the checked radio button make the corresponding spin box enable and make the rest of the spin box disable 5. Also assign the flag values according to the selected radio button 6. Add same action to all three spin boxes 7. Inside the spin box action check which spin box is enabled and make other spin box values zero 8. Add action to the push button 9. Inside the push button action check the flag according to the flag with the help of golden ratio formula calculate the other two lengths 10. Format the calculated values and show the values 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 datetimeimport sysÂ
Â
class Window(QMainWindow):Â
    def __init__(self):        super().__init__()Â
        # setting title        self.setWindowTitle("Python ")Â
        # width of window        self.w_width = 400Â
        # height of window        self.w_height = 430Â
        # 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("Golden Ratio Calculator", self)Â
        head.setWordWrap(True)Â
        # setting geometry to the head        head.setGeometry(0, 10, 400, 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 radio button        self.length1 = QRadioButton("First Length (A)", self)Â
        # setting geometry        self.length1.setGeometry(50, 90, 140, 40)Â
        # setting font        self.length1.setFont(QFont('Times', 9))Â
        # creating a spin box        self.l1 = QSpinBox(self)        self.l1.setMaximum(999999)Â
        # setting geometry to the spin box        self.l1.setGeometry(200, 90, 160, 40)Â
        # setting font        self.l1.setFont(QFont('Times', 9))Â
        # setting alignment        self.l1.setAlignment(Qt.AlignCenter)Â
        # creating a radio button        self.length2 = QRadioButton("Second Length (B)", self)Â
        # setting geometry        self.length2.setGeometry(50, 150, 145, 40)Â
        # setting font        self.length2.setFont(QFont('Times', 9))Â
        # creating a spin box        self.l2 = QSpinBox(self)        self.l2.setMaximum(999999)Â
        # setting geometry to the spin box        self.l2.setGeometry(200, 150, 160, 40)Â
        # setting font        self.l2.setFont(QFont('Times', 9))Â
        # setting alignment        self.l2.setAlignment(Qt.AlignCenter)Â
        # creating a radio button        self.length_sum = QRadioButton("First + Second ", self)Â
        # setting geometry        self.length_sum.setGeometry(50, 200, 140, 40)Â
        # setting font        self.length_sum.setFont(QFont('Times', 9))Â
        # creating a spin box        self.l_s = QSpinBox(self)        self.l_s.setMaximum(999999)Â
        # setting geometry to the spin box        self.l_s.setGeometry(200, 200, 160, 40)Â
        # setting font        self.l_s.setFont(QFont('Times', 9))Â
        # setting alignment        self.l_s.setAlignment(Qt.AlignCenter)Â
        # adding same action to all the radio button        self.length1.clicked.connect(self.radio_method)        self.length2.clicked.connect(self.radio_method)        self.length_sum.clicked.connect(self.radio_method)Â
        # adding same action to all the spin box        self.l1.valueChanged.connect(self.spin_method)        self.l2.valueChanged.connect(self.spin_method)        self.l_s.valueChanged.connect(self.spin_method)Â
        # making all the spin box disabled        self.l1.setDisabled(True)        self.l2.setDisabled(True)        self.l_s.setDisabled(True)Â
Â
Â
        # creating a push button        calculate = QPushButton("Calculate", self)Â
        # setting geometry to the push button        calculate.setGeometry(100, 270, 200, 40)Â
        # adding action to the button        calculate.clicked.connect(self.calculate)Â
        # adding color effect to the push button        color = QGraphicsColorizeEffect()        color.setColor(Qt.blue)        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(50, 330, 300, 70)Â
        # 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))Â
Â
    # method called by the radio buttons    def radio_method(self):Â
        # checking who is checked and who is unchecked        # if first radio button is checked        if self.length1.isChecked():Â
            # making first spin box enable            self.l1.setEnabled(True)Â
            # making rest two spin box disable            self.l2.setDisabled(True)            self.l_s.setDisabled(True)Â
Â
            # assigning flags            self.check1 = True            self.check2 = False            self.check_sum = FalseÂ
        elif self.length2.isChecked():Â
            # making second spin box enable            self.l2.setEnabled(True)Â
            # making rest two spin box disable            self.l1.setDisabled(True)            self.l_s.setDisabled(True)Â
Â
            # assigning flags            self.check1 = False            self.check2 = True            self.check_sum = FalseÂ
Â
        elif self.length_sum.isChecked():Â
            # making third spin box enable            self.l_s.setEnabled(True)Â
            # making rest two spin box disable            self.l1.setDisabled(True)            self.l2.setDisabled(True)Â
Â
            # assigning flags            self.check1 = False            self.check2 = False            self.check_sum = TrueÂ
    def spin_method(self):Â
        # finding who called the method        if self.l1.isEnabled():Â
            # setting current values            self.l2.setValue(0)            self.l_s.setValue(0)Â
Â
        elif self.l2.isEnabled():Â
            # setting current values            self.l1.setValue(0)            self.l_s.setValue(0)Â
        else:            # setting current values            self.l2.setValue(0)            self.l1.setValue(0)Â
    def calculate(self):Â
Â
Â
        golden = 1.61803398875Â
Â
        # if first value is selected        if self.check1 == True:Â
            # getting spin box value            A = self.l1.value()Â
            B = A / goldenÂ
            Sum = A + BÂ
        elif self.check2 == True:Â
            # getting spin box value            B = self.l2.value()Â
            A = B * goldenÂ
            Sum = A + BÂ
        else:            # getting spin box value            Sum = self.l_s.value()Â
            A = Sum / goldenÂ
            B = Sum - AÂ
Â
Â
        # formatting values upto two decimal        A = "{:.2f}".format(A)        B = "{:.2f}".format(B)        Sum = "{:.2f}".format(Sum)Â
        # setting text to the label        self.result.setText("A = " + str(A) + ", B = " + str(B) +                                        " and Sum = " + str(Sum))Â
Â
Â
Â
Â
# create pyqt5 appApp = QApplication(sys.argv)Â
# create the instance of our Windowwindow = Window()Â
# start the appsys.exit(App.exec()) |
Output :



