Created
September 13, 2021 07:16
-
-
Save Dhravya/ff469361f30f593e311257fdc7e03ef9 to your computer and use it in GitHub Desktop.
Basic calculator using tkinter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tkinter as tk | |
from tkinter import * | |
import math | |
from functools import reduce | |
def reverse(s): | |
str = "" | |
for i in s: | |
str = i + str | |
return str | |
def findroots(equation): | |
import cmath | |
posadd = equation.find('+') | |
possub = equation.find('-') | |
if '&' in equation: | |
n = equation.find('&') | |
a = equation[:n] | |
if 'x' in equation: | |
n = equation.find('x') | |
g = equation[posadd:n] | |
h = equation[possub:n] | |
try: | |
b = float(g) | |
except Exception as e: | |
b = float(h) | |
c = equation[n +1:] | |
a = float(a) | |
c = float(c) | |
print (a, b ,c) | |
# calculate the discriminant | |
d = (b**2) - (4*a*c) | |
# find two solutions | |
sol1 = (-b-cmath.sqrt(d))/(2*a) | |
sol2 = (-b+cmath.sqrt(d))/(2*a) | |
print('The solution are {0} and {1}'.format(sol1,sol2)) | |
return ('{0} and {1}'.format(sol1,sol2)) | |
def find_nth(haystack, needle, n): | |
start = haystack.find(needle) | |
while start >= 0 and n > 1: | |
start = haystack.find(needle, start+len(needle)) | |
n -= 1 | |
return start | |
def remove(string): | |
return string.replace(" ", "") | |
def Convert(lst): | |
return [ -i for i in lst if str(i) != str(i[0])] | |
def raisedToFinder (number, power): | |
value = 1 | |
for x in range(1, power+1): | |
value = number * value | |
print (value) | |
def click(event): | |
global scvalue | |
text = event.widget.cget("text") | |
text = remove(text) | |
# numbers = [1,2,3,4,5,6,7,8,9,0,'1','2','3','4','5','6','7','8','9','0'] | |
if text == "=": | |
b = scvalue.get() | |
b = remove(b) | |
if 'pi' in b: | |
b.replace('pi', '22/7') | |
if scvalue.get().isdigit(): | |
value = int(b) | |
elif '&' in scvalue.get() : | |
b = scvalue.get() | |
value = findroots(b) | |
elif 'sin' in scvalue.get(): | |
b = scvalue.get() | |
list = [] | |
print(b) | |
value = 0 | |
splitNegative = None | |
if '-' in b: | |
list = b.split('-') | |
print(list) | |
splitNegative = True | |
else : | |
list = b.split('+') | |
lastlist = [] | |
for a in list: | |
z = a.replace('sin', '') | |
i = math.sin(float(z)) | |
lastlist.append(i) | |
try: | |
if splitNegative == True : | |
value = lastlist[0] - lastlist[1] | |
else: | |
value = lastlist[0] + lastlist[1] | |
except Exception as e: | |
value= lastlist[0] | |
scvalue.set(value) | |
Answerlabel.update() | |
if 'cos' in scvalue.get(): | |
b = scvalue.get() | |
list = [] | |
print(b) | |
value = 0 | |
splitNegative = None | |
if '-' in b: | |
list = b.split('-') | |
print(list) | |
splitNegative = True | |
else : | |
list = b.split('+') | |
lastlist = [] | |
for a in list: | |
z = a.replace('cos', '') | |
i = math.cos(float(z)) | |
lastlist.append(i) | |
try: | |
if splitNegative == True : | |
value = lastlist[0] - lastlist[1] | |
else: | |
value = lastlist[0] + lastlist[1] | |
except Exception as e: | |
value= lastlist[0] | |
scvalue.set(value) | |
Answerlabel.update() | |
else: | |
try: | |
value = eval(Answerlabel.get()) | |
except Exception as e: | |
print(e) | |
value = "Error" | |
scvalue.set(value) | |
Answerlabel.update() | |
elif text == "C": | |
scvalue.set("") | |
Answerlabel.update() | |
elif text == '<': | |
b = scvalue.get() | |
size = len(b) | |
a = b[:size-1] | |
scvalue.set(a) | |
else: | |
scvalue.set(scvalue.get() + text) | |
Answerlabel.update() | |
root = tk.Tk() | |
root.title('Calculator') | |
root.geometry('800x450') | |
root.configure(bg='#3e4f5e') | |
scvalue = StringVar() | |
scvalue.set("") | |
b = Button(root , padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= '1' , font=('helvetica', 20, 'bold')) | |
b.place(x= 75 , y=280) | |
b.bind("<Button-1>", click) | |
b = Button(root , padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= '2' , font=('helvetica', 20, 'bold')) | |
b.place(x= 150 , y=280) | |
b.bind("<Button-1>", click) | |
b = Button(root , padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= '3' , font=('helvetica', 20, 'bold')) | |
b.place(x= 225 , y=280) | |
b.bind("<Button-1>", click) | |
b = Button(root , padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= '4' , font=('helvetica', 20, 'bold')) | |
b.place(x= 75 , y=200) | |
b.bind("<Button-1>", click) | |
b = Button(root , padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= '5' , font=('helvetica', 20, 'bold')) | |
b.place(x= 150 , y=200) | |
b.bind("<Button-1>", click) | |
b = Button(root , padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= '6' , font=('helvetica', 20, 'bold')) | |
b.place(x= 225 , y=200) | |
b.bind("<Button-1>", click) | |
b = Button(root , padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= '7' , font=('helvetica', 20, 'bold')) | |
b.place(x= 75 , y=120) | |
b.bind("<Button-1>", click) | |
b = Button(root , padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= '8' , font=('helvetica', 20, 'bold')) | |
b.place(x= 150 , y=120) | |
b.bind("<Button-1>", click) | |
b = Button(root , padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= '9' , font=('helvetica', 20, 'bold')) | |
b.place(x= 225 , y=120) | |
b.bind("<Button-1>", click) | |
b = Button(root , padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= '0' , font=('helvetica', 20, 'bold')) | |
b.place(x= 75 , y=360) | |
b.bind("<Button-1>", click) | |
b = Button(root , padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= '00' , font=('helvetica', 20, 'bold')) | |
b.place(x= 150 , y=360) | |
b.bind("<Button-1>", click) | |
b = Button(root , padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= '.' , font=('helvetica', 20, 'bold')) | |
b.place(x= 240 , y=360) | |
b.bind("<Button-1>", click) | |
add = tk.Button(root, padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= '+' , font=('helvetica', 20, 'bold')) | |
add.place(x= 320 , y=200) | |
add.bind("<Button-1>", click) | |
sub = tk.Button(root, padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= '- ' , font=('helvetica', 20, 'bold')) | |
sub.place(x= 320 , y=280) | |
sub.bind("<Button-1>", click) | |
b = Button(root , padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= ' C ' , font=('helvetica', 20, 'bold')) | |
b.place(x= 320 , y=120) | |
b.bind("<Button-1>", click) | |
sub = tk.Button(root, padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= ' *' , font=('helvetica', 20, 'bold')) | |
sub.place(x= 405 , y=280) | |
sub.bind("<Button-1>", click) | |
sub = tk.Button(root, padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= '/ ' , font=('helvetica', 20, 'bold')) | |
sub.place(x= 405 , y=200) | |
sub.bind("<Button-1>", click) | |
sub = tk.Button(root, padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= ' = ' , font=('helvetica', 20, 'bold')) | |
sub.place(x= 320, y=360) | |
sub.bind("<Button-1>", click) | |
back= tk.Button(root, padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= '<' , font=('helvetica', 20, 'bold')) | |
back.place(x= 490, y=120) | |
back.bind("<Button-1>", click) | |
back= tk.Button(root, padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= '&' , font=('helvetica', 20, 'bold')) | |
back.place(x= 490, y=200) | |
back.bind("<Button-1>", click) | |
back= tk.Button(root, padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= 'x' , font=('helvetica', 20, 'bold')) | |
back.place(x= 490, y=280) | |
back.bind("<Button-1>", click) | |
sin= tk.Button(root, padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= 'sin' , font=('helvetica', 20, 'bold')) | |
sin.place(x= 570, y=120) | |
sin.bind("<Button-1>", click) | |
sin= tk.Button(root, padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= 'cos' , font=('helvetica', 20, 'bold')) | |
sin.place(x= 570, y=200) | |
sin.bind("<Button-1>", click) | |
sin= tk.Button(root, padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= 'tan' , font=('helvetica', 20, 'bold')) | |
sin.place(x= 570, y=280) | |
sin.bind("<Button-1>", click) | |
back= tk.Button(root, padx=15 , pady = 10, activebackground= '#adb9cc' , relief='raised' , text= 'pi' , font=('helvetica', 20, 'bold')) | |
back.place(x= 490, y=360) | |
back.bind("<Button-1>", click) | |
scValue = StringVar() | |
scValue.set('') | |
Answerlabel = tk.Entry(root, textvariable=scvalue ,font =('Arial', 25) ,bg = '#61ab73', bd=2) | |
Answerlabel.place(x= 120,y=20) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment