Silver Ratio Calculator using PyQt5

In this article we will see how we can create a silver ratio calculator using PyQt5. In mathematics, two quantities are in the silver ratio if the ratio of the sum of the smaller and twice the larger of those quantities, to the larger quantity, is the same as the ratio of the larger one to the smaller one. Below is how the silver ratio calculator will look like

PyQt5 is cross-platform GUI toolkit, a set of python bindings for Qt v5. One can develop an interactive desktop application with so much ease because of the tools and simplicity provided by this library. Below is the command to install the PyQt5

pip install PyQt5

Concept :
Below is the formula for calculating silver ratio

A/B = 1 + ?2

Here A is the larger length and B is the shorter i.e second part of the length, both A and B should be greater than zero

GUI Implementation Steps :
1. Create a heading label that display the calculator name
2. Create two radio buttons for selecting between first and second length
3. Create two spin boxes for user to enter the specific length
4. Create push button for calculating the other values according tot 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 both the radio button
3. Inside the radio button method check which radio button is checked
4. According to the checked radio button make the corresponding spin box enable and make the other the spin box disable
5. Also assign the flag vales according to the selected radio button
6. Add same action to both spin boxes
7. Inside the spin box action check which spin box is enabled and make other spin box value 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 silver ratio formula calculate the other length
10. Format the calculated length and show the length with the help of result label

Below is the implementation





# importing libraries
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):
        # 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
        # showing all the widgets
    # method for components
    def UiComponents(self):
        # creating head label
        head = QLabel("Silver Ratio Calculator", self)
        # setting geometry to the head
        head.setGeometry(0, 10, 400, 60)
        # font
        font = QFont('Times', 15)
        # setting font to the head
        # setting alignment of the head
        # setting color effect to the head
        color = QGraphicsColorizeEffect(self)
        # 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)
        # setting geometry to the spin box
        self.l1.setGeometry(200, 90, 160, 40)
        # setting font
        self.l1.setFont(QFont('Times', 9))
        # setting alignment
        # 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)
        # setting geometry to the spin box
        self.l2.setGeometry(200, 150, 160, 40)
        # setting font
        self.l2.setFont(QFont('Times', 9))
        # setting alignment
        # adding same action to all the radio button
        # adding same action to all the spin box
        # making all the spin box disabled
        # 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
        # adding color effect to the push button
        color = QGraphicsColorizeEffect()
        # creating a label to show result
        self.result = QLabel(self)
        # setting properties to result label
        # setting geometry
        self.result.setGeometry(50, 330, 300, 70)
        # making it multi line
        # setting stylesheet
        # adding border and background
                                  "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
            # making rest two spin box disable
            # assigning flags
            self.check1 = True
            self.check2 = False
        elif self.length2.isChecked():
            # making second spin box enable
            # making rest two spin box disable
            # assigning flags
            self.check1 = False
            self.check2 = True
    def spin_method(self):
        # finding who called the method
        if self.l1.isEnabled():
            # setting current values
        elif self.l2.isEnabled():
            # setting current values
    def calculate(self):
        # silver ratio value
        silver = 2.414
        # if first value is selected
        if self.check1 == True:
            # getting spin box value
            A = self.l1.value()
            # calculating smaller length
            B = A / silver
        elif self.check2 == True:
            # getting spin box value
            B = self.l2.value()
            # calculating larger length
            A = B * silver
        # formatting values upto two decimal
        A = "{:.2f}".format(A)
        B = "{:.2f}".format(B)
        # setting text to the label
        self.result.setText("A = " + str(A) + ", B = " + str(B))
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app


Output :

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.