A calculator is an object of everyday use and none of us can imagine a life without it.

Therefore, I decided to make a calculator of my own using pyqt5.

PyQt5 is a set of python bindings for the GUI toolkit Qt. It is used as an alternative application development language to C++ on all supported platforms like iOS and android.

The GUI part of the calculator was made using the designer app of PyQt5.

The back-end functions were written in python.

This is a screenshot of how the app looks:

I used the different buttons to create a string containing the equation.

For example: In the above situation, The string I need is “1+1”

Next, I use the eval() function to find the answer to the equation stored in the string.

However, a problem arises. The eval() function in python does not recognise “√” and “^”. Hence, These will cause an error and will stop the program.

I got around this problem using string splicing.

I searched for these symbols and replaced those sections of string with the  values of the squares or roots.

For example: The following string: “1 + 2^2 – √4”, would be edited using the program and would result in “1 + 4 – 2”

Following is the program used in editing the string:

def clickEq(self):
        s = self.Output.text()
        i = 0
        flag = True
        while i < len(s):
            if s[i] == "^" or s[i] == "√" : // searching for the symbols
                if s[i] == "^":
                    //splicing the string according to the “^” format
                    st = s[:i]
                    k = 0
                    for j in range(len(st)-1,0,-1):
                        if st[j] == " ":
                            k = j
                            break
                    if st[k+1:] != "":
                        st = st[:k] + " " +str(pow(int(st[k:]),2)) + " "
                    else:
                        st = str(pow(int(st[k:]),2))
                    s = st + s[i+3:]
                
                elif s[i] == "√":
			             //splicing the string according to the “√” format
                    sb = s[:i]
                    sf = s[i+1:]
                    k = 0
                    print (sf)
                    if sf[0] == "-":
			                  //avoiding the root of negative numbers fiasco
                        flag = False
                        break
                    for j in range(len(sf)):
                        if sf[j] == " " or j == len(sf)-1:
                            k = j
                            break
                    print(k)
                    st = sf[:k+1]
                    sf = sf[k+1:]
                    print(st)
                    a = math.sqrt(int(st))
                    print("a = ",a)
                    s = sb + str(a) + sf

            i+=1
        if flag == True:
            ans = str(eval(s)) // calculating the answer
            if float(ans) > 1000000000.0:
		            // making sure the final number is not too big
                font = QtGui.QFont()
                font.setPointSize(17)
                self.Output.setFont(font)
                self.Output.setText("Math Error: Number too large")
            elif len(ans) > 10:
		            // making sure there are not too many decimal places
                ans = ans[:10]
                self.Output.setText(ans)
            else:
                self.Output.setText(ans)
            print(eval(s))
        else:
		        //avoiding the root of negative numbers fiasco
            self.Output.setText("ERROR")

Output is the name of the label used to display the answer. The function for linking a button click to a set function is given below:

self.equal.clicked.connect(self.clickEq)

I have used “equal” as the name of the button that is supposed to print the answer of the equation.

Following are some screenshots of the calculator working:

The github link for the full program is:

https://github.com/HSNA243/PythonPrograms/blob/master/CalculatorPY.py

Visits: 83

Leave a Reply

Your email address will not be published. Required fields are marked *