Tkinterで試しに電卓を作ってみたので書いてみる

#1.作るきっかけ
電卓を自作してみたいと考えていたから。
#2.作る材料
Python 3.8
Tkinterとmath
基本的に使ったのはこれだけ。
#3.苦労した点
桁上がりの処理と、複数の項の計算の実装(今のところ、対応しているのは足し算だけですが)
#4.実装コード
4-1:イントロ-GUI表示
今回はtkinterを用い、GUIを作成した。
以下コード
※備忘録程度に書いています。適宜かみ砕いて修正していきます。

```python:main.py
import tkinter as tk
import math
root=tk.Tk()
#root.minsize(640,480)
#可変テスト
root.geometry('538x720')
root.resizable(width=0, height=0)
canvas = tk.Canvas(root, width = 538, height = 720,bg = "black")
#キャンバスバインド
canvas.place(x=0, y=0)

#StringVar()

#桁上がりチェック
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
    #まず一桁分確保
    keisan = 0
    flag = False
    result.set('Enter.')


a = 0
b= 0
#オペランド0=足し算,1=引き算,2=掛け算,3=割り算
op = 0
#加算分リスト
alist = []
def plus():
    global output
    global a
    global op
    global alist
    op =0
    map(str, output)
    #joinで数字を結合
    ketsugou = "".join(map(str, output)) 
    a = int(ketsugou)
    print(a)
    alist.append(a)
    print(alist)
    print(len(alist))
    #リストを削除
    output.clear() 

def minus():
    global output
    global a
    global op
    global alist
    op =1
    map(str, output)
    #joinで数字を結合
    ketsugou = "".join(map(str, output)) 
    a = int(ketsugou)
    print(a)
    alist.append(a)
    print(alist)
    print(len(alist))
    #リストを削除
    output.clear() 

def kakeru():
    global output
    global a
    global op
    global alist
    op =2
    map(str, output)
    #joinで数字を結合
    ketsugou = "".join(map(str, output)) 
    a = int(ketsugou)
    print(a)
    alist.append(a)
    print(alist)
    print(len(alist))
    #リストを削除
    output.clear()



def waru():
    global output
    global a
    global op
    global alist
    op =3
    map(str, output)
    #joinで数字を結合
    ketsugou = "".join(map(str, output)) 
    a = int(float(ketsugou))
    print(a)
    alist.append(a)
    print(alist)
    print(len(alist))
    #リストを削除
    output.clear() 

def rote():
    global output
    global a
    global op
    global result
    op =4
    map(str, output)
    #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)
    #リストを削除
    output.clear()

def seigen():
    global output
    global a
    global op
    if str(a) == "":
        result.set("Error!")
    else:
        op =5
        map(str, output)
        #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))
    #リストを削除
    output.clear()


def yogen():
    global output
    global a
    global op
    op =6
    map(str, output)
    #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))
    #リストを削除
    output.clear()

def seisetsu():
    global output
    global a
    global op
    op =7
    map(str, output)
    #joinで数字を結合
    ketsugou = "".join(map(str, output)) 
    a = float(ketsugou)
    tana = math.tan(math.radians(a))
    print(tana)
    #45の倍数の時の条件分岐
    if a%45 == 0:
        if a % 90 == 0:
            result.set("Divison Error!")
        elif a % 180 == 0:
            result.set("0")
        #-1と1の時の条件分岐が式不明
    #+90,+270の時が-
    if a == 45:
        result.set("1")
    if a == 135:
        result.set("-1")

    else:
        result.set(str(tana))
    #リストを削除
    output.clear()

#2乗
def jo():
    global output
    global a
    global op
    global alist
    op =8
    map(str, output)
    #joinで数字を結合
    ketsugou = "".join(map(str, output)) 
    a = float(ketsugou)
    result.set(str(int(a)))
    #リストを削除
    output.clear()
    alist.append(a)

#ln
def logu():
    global output
    global a
    global op
    global alist
    op =9
    map(str, output)
    #joinで数字を結合
    ketsugou = "".join(map(str, output)) 
    a = float(ketsugou)
    lo = math.log(a)
    result.set(lo)
    #リストを削除
    output.clear()
    alist.append(a)

def ans():
    global output
    global a
    global op
    global alist
    op=10

wa = 0
#オペランド0=足し算,1=引き算,2=掛け算,3=割り算
#合計を格納
keilist =[]
def equal():
    global output
    global b
    global a
    global wa
    global op
    global alist
    global keilist
    global c
    #最後の数
    map(str, output)
    mix = "".join(map(str, output)) #joinで数字を結合
    b = int(mix)
    if op == 0:
        c=0
        for n in range(0,len(alist)):
            c+=alist[n]
        print("現在のaの合計"+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("掛け算の結果:"+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("現在の阿知あ"+(lo))
        result.set(lo)
    a=0
    b=0
    c=0
    op=0
    output.clear()
    alist.clear()

#以下ボタン
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)
#height1ずらすと20ずれる,9で94の間隔
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)
#答えを記憶
da = tk.Button(text=u'Ans', width=10,height=4,command=equal)
da.place(x=445,y=625)

#計算表示ラベル

result_l = tk.Label(textvariable=result,font=("DSEG7 Classic", "18"),fg="orange",bg="black",anchor = 'e',width=45) #寄せるときはwidthを設定しないとanchorが有効にならない
result_l.place(x=-580,y=25)



root.mainloop()
```
プログラミングを始めてまだ9か月の超初心者が書いたコードです。経験者から見たら余計なコードが多いと思います。

#5.考察
市販の関数電卓は高機能で低価格。市販品ってすごいなと思った。
#6.参考サイト
三角関数をpythonで使うには https://note.nkmk.me/python-math-sin-cos-tan/