I wanted to make my own calculator.
Python 3.8 Tkinter and math This is the only one I basically used.
Carry processing and implementation of multi-term calculation (although only addition is currently supported)
4-1: Intro-GUI display This time, I created a GUI using tkinter. Code below
main.py
import tkinter as tk
import math
root=tk.Tk()
#root.minsize(640,480)
#Variable test
root.geometry('538x720')
root.resizable(width=0, height=0)
canvas = tk.Canvas(root, width = 538, height = 720,bg = "black")
#Canvas bind
canvas.place(x=0, y=0)
#StringVar()
#Carry check
up = 0
keisan = 0
output = []
jou = 0
result= tk.StringVar()
result.set(keisan)
def ichi():
global result
global output
global x
x = 1
output.append(x)
print(output)
result.set(output)
print(len(output))
def ni():
global result
global output
global x
x = 2
output.append(x)
print(output)
result.set(output)
print(len(output))
def san():
global result
global output
global x
x = 3
output.append(x)
print(output)
result.set(output)
print(len(output))
def yon():
global result
global output
global x
x = 4
output.append(x)
print(output)
result.set(output)
print(len(output))
def go():
global result
global output
global x
x = 5
output.append(x)
print(output)
result.set(output)
print(len(output))
def roku():
global result
global output
global x
x = 6
output.append(x)
print(output)
result.set(output)
print(len(output))
def nana():
global result
global output
global x
x = 7
output.append(x)
print(output)
result.set(output)
print(len(output))
def hachi():
global result
global output
global x
x = 8
output.append(x)
print(output)
result.set(output)
print(len(output))
def kyu():
global result
global output
global x
x = 9
output.append(x)
print(output)
result.set(output)
print(len(output))
def zer():
global result
global output
global x
x = 0
output.append(x)
print(output)
result.set(output)
print(len(output))
def delete():
global up
global result
global keisan
global jou
global flag
#First, secure one digit
keisan = 0
flag = False
result.set('Enter.')
a = 0
b= 0
#Operand 0=addition,1=subtraction,2=multiplication,3=division
op = 0
#Addition list
alist = []
def plus():
global output
global a
global op
global alist
op =0
map(str, output)
#Join numbers with join
ketsugou = "".join(map(str, output))
a = int(ketsugou)
print(a)
alist.append(a)
print(alist)
print(len(alist))
#Delete list
output.clear()
def minus():
global output
global a
global op
global alist
op =1
map(str, output)
#Join numbers with join
ketsugou = "".join(map(str, output))
a = int(ketsugou)
print(a)
alist.append(a)
print(alist)
print(len(alist))
#Delete list
output.clear()
def kakeru():
global output
global a
global op
global alist
op =2
map(str, output)
#Join numbers with join
ketsugou = "".join(map(str, output))
a = int(ketsugou)
print(a)
alist.append(a)
print(alist)
print(len(alist))
#Delete list
output.clear()
def waru():
global output
global a
global op
global alist
op =3
map(str, output)
#Join numbers with join
ketsugou = "".join(map(str, output))
a = int(float(ketsugou))
print(a)
alist.append(a)
print(alist)
print(len(alist))
#Delete list
output.clear()
def rote():
global output
global a
global op
global result
op =4
map(str, output)
#Join numbers with join
ketsugou = "".join(map(str, output))
a = float(ketsugou)
r= a**(0.5)
if a == "":
result.set("Error!")
else:
result.set(str(r))
print(a)
#Delete list
output.clear()
def seigen():
global output
global a
global op
if str(a) == "":
result.set("Error!")
else:
op =5
map(str, output)
#Join numbers with join
ketsugou = "".join(map(str, output))
a = float(ketsugou)
sina = math.sin(math.radians(a))
print(sina)
if a == 90.0:
result.set("1")
else:
result.set(str(sina))
#Delete list
output.clear()
def yogen():
global output
global a
global op
op =6
map(str, output)
#Join numbers with join
ketsugou = "".join(map(str, output))
a = float(ketsugou)
cosa = math.cos(math.radians(a))
print(cosa)
if a == "":
result.set("Error!")
if a == 90.0:
result.set("0")
else:
result.set(str(cosa))
#Delete list
output.clear()
def seisetsu():
global output
global a
global op
op =7
map(str, output)
#Join numbers with join
ketsugou = "".join(map(str, output))
a = float(ketsugou)
tana = math.tan(math.radians(a))
print(tana)
#Conditional branching when multiples of 45
if a%45 == 0:
if a % 90 == 0:
result.set("Divison Error!")
elif a % 180 == 0:
result.set("0")
#-The conditional branch at the time of 1 and 1 is unknown
#+90,+The time of 270-
if a == 45:
result.set("1")
if a == 135:
result.set("-1")
else:
result.set(str(tana))
#Delete list
output.clear()
#Square
def jo():
global output
global a
global op
global alist
op =8
map(str, output)
#Join numbers with join
ketsugou = "".join(map(str, output))
a = float(ketsugou)
result.set(str(int(a)))
#Delete list
output.clear()
alist.append(a)
#ln
def logu():
global output
global a
global op
global alist
op =9
map(str, output)
#Join numbers with join
ketsugou = "".join(map(str, output))
a = float(ketsugou)
lo = math.log(a)
result.set(lo)
#Delete list
output.clear()
alist.append(a)
def ans():
global output
global a
global op
global alist
op=10
wa = 0
#Operand 0=addition,1=subtraction,2=multiplication,3=division
#Store total
keilist =[]
def equal():
global output
global b
global a
global wa
global op
global alist
global keilist
global c
#Last number
map(str, output)
mix = "".join(map(str, output)) #Join numbers with join
b = int(mix)
if op == 0:
c=0
for n in range(0,len(alist)):
c+=alist[n]
print("Current a total"+str(c))
wa = c+b
alist.clear()
alist.append(wa)
result.set(wa)
if op == 1:
c=0
for z in range(0,len(alist)):
c-=alist[z]
sa = c-b
alist.clear()
result.set(sa)
if op == 2:
c=0
for q in range(0,len(alist)):
c+=alist[q]
alist.clear()
ka = c*b
print("The result of multiplication:"+str(ka))
result.set(ka)
if op ==3:
if b == 0:
result.set("Divison Error!")
else:
c=0
for u in range(0,len(alist)):
c+=alist[u]
alist.clear()
ru = c/b
result.set(float(ru))
if op ==8:
jou = a ** (b)
alist.clear()
alist.append(jou)
result.set(str(int(jou)))
if op ==9:
lo = math.log(a)
print("Current Achi"+(lo))
result.set(lo)
a=0
b=0
c=0
op=0
output.clear()
alist.clear()
#Below button
seven = tk.Button(text=u'7', width=10,height=4,command=nana)
seven.place(x=0,y=343)
eight = tk.Button(text=u'8', width=10,height=4,command=hachi)
eight.place(x=89,y=343)
nine = tk.Button(text=u'9', width=10,height=4,command=kyu)
nine.place(x=178,y=343)
#If height1 is shifted, it will be shifted by 20.,9 to 94 intervals
four = tk.Button(text=u'4', width=10,height=4,command=yon)
four.place(x=0,y=437)
five = tk.Button(text=u'5', width=10,height=4,command=go)
five.place(x=89,y=437)
six = tk.Button(text=u'6', width=10,height=4,command=roku)
six.place(x=178,y=437)
one = tk.Button(text=u'1', width=10,height=4,command=ichi)
one.place(x=0,y=531)
two = tk.Button(text=u'2', width=10,height=4,command=ni)
two.place(x=89,y=531)
three = tk.Button(text=u'3', width=10,height=4,command=san)
three.place(x=178,y=531)
zero = tk.Button(text=u'0', width=10,height=4,command=zer)
zero.place(x=0,y=625)
dele = tk.Button(text=u'DEL', width=10,height=4,command=delete)
dele.place(x=89,y=625)
ac = tk.Button(text=u'AC', width=10,height=4,command=delete)
ac.place(x=178,y=625)
kake = tk.Button(text=u'×', width=10,height=4,command=kakeru)
kake.place(x=267,y=343)
war = tk.Button(text=u'÷', width=10,height=4,command=waru)
war.place(x=267,y=437)
pls = tk.Button(text=u'+', width=10,height=4,command=plus)
pls.place(x=267,y=531)
mns = tk.Button(text=u'-', width=10,height=4,command=minus)
mns.place(x=267,y=625)
sin = tk.Button(text=u'sin', width=10,height=4,command=seigen)
sin.place(x=356,y=343)
cos = tk.Button(text=u'cos', width=10,height=4,command=yogen)
cos.place(x=356,y=437)
rot = tk.Button(text=u'√', width=10,height=4,command=rote)
rot.place(x=356,y=531)
eq = tk.Button(text=u'=', width=10,height=4,command=equal)
eq.place(x=356,y=625)
tang = tk.Button(text=u'tan', width=10,height=4,command=seisetsu)
tang.place(x=445,y=343)
nijou = tk.Button(text=u'^', width=10,height=4,command=jo)
nijou.place(x=445,y=437)
log = tk.Button(text=u'ln', width=10,height=4,command=logu)
log.place(x=445,y=531)
#Remember the answer
da = tk.Button(text=u'Ans', width=10,height=4,command=equal)
da.place(x=445,y=625)
#Calculation label
result_l = tk.Label(textvariable=result,font=("DSEG7 Classic", "18"),fg="orange",bg="black",anchor = 'e',width=45) #Anchor is not enabled unless width is set when pulling
result_l.place(x=-580,y=25)
root.mainloop()
This code was written by a super beginner who has only been programming for 9 months. From an experienced person's point of view, there is a lot of extra code.
Commercially available scientific calculators are highly functional and inexpensive. I thought the commercial product was amazing.
To use trigonometric functions in python https://note.nkmk.me/python-math-sin-cos-tan/
Recommended Posts