beginner11
@beginner11

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

計算ツールを作成したい

分からないことについて

Pythonで計算ツールを作成したいのですが、上手くいきません。
VSCodeで記述しています。
1つ目の数字、記号、2つ目の数字を組み合わせて、適切な計算結果を表示したいです。

エラーの内容について

スクリーンショット 2025-02-09 130827.png

該当するソースコード

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

2Answer

「エラーの内容について」の箇所が正しく表示されていません。修正お願いします。

下記、コードについて気になった点です。

  • ans関数内では関数を定義しているのみのようです。
  • 「kotae」という変数がsum関数のみで使われていますがこれは意図したものですか?
  • 変数answerに数値を代入しているようですが良いのでしょうか?
  • 「num_two=int(two.get())」のコードがelse節にもあるのはどういう意図ですか?

tkinterを使って同じような計算ツールの作成例について、色々と調べてみましたが、どうしても分かりません。

tkinterの使い方というよりもPythonの書き方(インデント、変数の使い方)の問題な気がします。
掲載のコードではans関数内のインデントがおかしいと思うのでまずは見直してください。

1Like

Comments

  1. @beginner11

    Questioner

    @megchandesuさんご指摘ありがとうございます!
    エラー内容のスクショ画像の方は表示しておきました。

  • mark : 記号を入力するテキストボックスを表す変数
  • mark_in : markに入力された文字列を格納した変数
    として、使用する想定だと理解しました。
    一方で実際のコードにおいて、calc関数やtan関数内でmarkと文字列を比較しています。
    mark_inと比較すべきではないでしょうか?
0Like

Your answer might help someone💌