計算ツールを作成したい
分からないことについて
Pythonで計算ツールを作成したいのですが、上手くいきません。
VSCodeで記述しています。
1つ目の数字、記号、2つ目の数字を組み合わせて、適切な計算結果を表示したいです。
エラーの内容について
該当するソースコード
brain_calc.py
import tkinter
import math
root=tkinter.Tk()
root.title("偉大なる頭脳")
root.minsize(600,580)
root.option_add("*font",["MS ゴシック",20])
canvas=tkinter.Canvas(bg="#99ff22",width=700,height=480)
canvas.place(x=0,y=0)
def calc():
global num_one
global mark_in
global num_two
global answer
num_one=int(one.get())
mark_in=str(mark.get())
num_two=int(two.get())
if mark=='pi' or mark=='log2' or mark=='sqrt' or mark=='sin' or mark=='cos' or mark=='tan':
ans()
entry.delete(0,tkinter.END)
entry.insert(0,answer)
else:
num_two=int(two.get())
ans()
entry.delete(0,tkinter.END)
entry.insert(0,answer)
def ans():
def sum():
kotae=int(num_one+num_two)
kotae["text"]=int(answer)
def minus():
answer=(num_one-num_two)
answer["text"]=int(answer)
def inc():
answer=(num_one*num_two)
answer["text"]=int(answer)
def div():
answer=(num_one/num_two)
answer["text"]=int(answer)
def div2():
ans=(num_one//num_two)
ans["text"]=str(ans)
def joe():
ans=(num_one**num_two)
ans["text"]=str(ans)
def extra():
ans=(num_one%num_two)
ans["text"]=str(ans)
def circle():
ans=(round(math.pi**num_one,2))
ans["text"]=str(ans)
def bottom():
ans=((math.log2(num_one)))
ans["text"]=str(ans)
def sqrt():
ans=((math.sqrt(num_one)))
ans["text"]=str(ans)
def sin():
ans=(round(math.sin(math.radians(num_one))))
ans["text"]=str(ans)
def cos():
ans=(round(math.cos(math.radians(num_one))))
ans["text"]=str(ans)
def tan():
ans=(math.tan(math.radians(num_one)))
ans["text"]=str(ans)
if mark=='+':
sum()
elif mark=='-':
minus()
elif mark=='*':
inc()
elif mark=='/':
div()
elif mark=='//':
div2()
elif mark=='**':
joe()
elif mark=='%':
extra()
elif mark=='pi':
circle()
elif mark=='log2':
bottom()
elif mark=='sqrt':
sqrt()
elif mark=='sin':
sin()
elif mark=='cos':
cos()
elif mark=='tan':
tan()
guide=tkinter.Label(text="1つ目の数字を入力してね",bg="#99ff22")
guide.place(x=80,y=50)
one=tkinter.Entry(width=12,bd=4)
one.place(x=400,y=50)
guide2=tkinter.Label(text="記号",bg="#99ff22")
guide2.place(x=80,y=150)
mark=tkinter.Entry(width=12,bd=4)
mark.place(x=400,y=150)
guide3=tkinter.Label(text="2つ目の数字を入力してね",bg="#99ff22")
guide3.place(x=80,y=250)
two=tkinter.Entry(width=12,bd=4)
two.place(x=400,y=250)
guide4=tkinter.Label(text="回答",bg="#99ff22")
guide4.place(x=80,y=350)
answer=tkinter.Entry(width=12,bd=4)
answer.pack(pady=10)
answer.place(x=400,y=350)
askbutton=tkinter.Button(master=root,text="計算する",command=calc)
askbutton.place(x=240,y=350)
entry=tkinter.Entry(master=root,width=12,bd=4)
entry.place(x=400,y=350)
root.mainloop()
調べたことなど
tkinterを使って同じような計算ツールの作成例について、色々と調べてみましたが、どうしても分かりません。どなたかアドバイスいただけると助かります。
追記
画像が正常に表示されていなかったので、修正しておきました。
(2025年2月9日 16時57分より)
0