I tried to make a calculator with Tkinter so I will write it

1. How to make

I wanted to make my own calculator.

2. Materials to make

Python 3.8 Tkinter and math This is the only one I basically used.

3. Difficulties

Carry processing and implementation of multi-term calculation (although only addition is currently supported)

4. Implementation code

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.

5. Consideration

Commercially available scientific calculators are highly functional and inexpensive. I thought the commercial product was amazing.

6. Reference site

To use trigonometric functions in python https://note.nkmk.me/python-math-sin-cos-tan/

Recommended Posts

I tried to make a calculator with Tkinter so I will write it
I tried to make a simple mail sending application with tkinter of Python
When I tried to make a VPC with AWS CDK but couldn't make it
I tried to make a stopwatch using tkinter in python
I tried to make GUI tic-tac-toe with Python and Tkinter
A Python beginner made a chat bot, so I tried to summarize how to make it.
I tried to make a Web API
[5th] I tried to make a certain authenticator-like tool with python
[2nd] I tried to make a certain authenticator-like tool with python
A memorandum when I tried to get it automatically with selenium
[Python] I tried to implement stable sorting, so make a note
[3rd] I tried to make a certain authenticator-like tool with python
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I made a segment tree with python, so I will introduce it
I tried to make a strange quote for Jojo with LSTM
I tried to make a mechanism of exclusive control with Go
There was a doppelganger, so I tried to distinguish it with artificial intelligence (laughs) (Part 1)
Python: I tried to make a flat / flat_map just right with a generator
I want to make a game with Python
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I want to write to a file with Python
I tried to make a skill that Alexa will return as cold
[Zaif] I tried to make it easy to trade virtual currencies with Python
I tried playing with the calculator on tkinter
I tried to make a url shortening service serverless with AWS CDK
I tried to make a ○ ✕ game using TensorFlow
I was addicted to trying Cython with PyCharm, so make a note
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
[Patent analysis] I tried to make a patent map with Python without spending money
When I tried to create a virtual environment with Python, it didn't work
I want to write an element to a file with numpy and check it.
I was able to mock AWS-Batch with python, moto, so I will leave it
Load a photo and make a handwritten sketch. With zoom function. Tried to make it.
I tried to make Kana's handwriting recognition Part 3/3 Cooperation with GUI using Tkinter
I tried to make a simple image recognition API with Fast API and Tensorflow
I set up TensowFlow and was addicted to it, so make a note
I tried to make a "fucking big literary converter"
I tried to create a table only with Django
I tried to draw a route map with Python
I tried to automatically generate a password with Python3
I tried to make an OCR application with PySimpleGUI
In IPython, when I tried to see the value, it was a generator, so I came up with it when I was frustrated.
I wanted to know the number of lines in multiple files, so I tried to get it with a command
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
I tried to make creative art with AI! I programmed a novelty! (Paper: Creative Adversarial Network)
A beginner tried coloring line art with chainer. I was able to do it.
I wanted to operate google spread sheet with AWS lambda, so I tried it [Part 2]
I tried to implement a volume moving average with Quantx
I tried to make various "dummy data" with Python faker
I want to make a blog editor with django admin
I want to make a click macro with pyautogui (desire)
I tried to solve a combination optimization problem with Qiskit
I want to make a click macro with pyautogui (outlook)
I tried to get started with Hy ・ Define a class
I stumbled when I tried to install Basemap, so a memorandum
I tried to sort a random FizzBuzz column with bubble sort.