計算機



講師から一言
小6でこれが作れるとはスゴイの一言です!特に下の一行のコードにはびっくりさせられました。

box = label[“text”]

get命令を使わず、電卓にいれた数字をとってきている点に脱帽です。



Pythonで記述したコード
import tkinter

def click_btn1():
    if label["text"] == "0":
        label["text"] = "1"
    else:
        label["text"] = label["text"] +"1"
def click_btn2():
    if label["text"] == "0":
        label["text"] = "2"
    else:
        label["text"] = label["text"] +"2"
def click_btn3():
    if label["text"] == "0":
        label["text"] = "3"
    else:
        label["text"] = label["text"] +"3"
def click_btn4():
    if label["text"] == "0":
        label["text"] = "4"
    else:
        label["text"] = label["text"] +"4"
def click_btn5():
    if label["text"] == "0":
        label["text"] = "5"
    else:
        label["text"] = label["text"] +"5"
def click_btn6():
    if label["text"] == "0":
        label["text"] = "6"
    else:
        label["text"] = label["text"] +"6"
def click_btn7():
    if label["text"] == "0":
        label["text"] = "7"
    else:
        label["text"] = label["text"] +"7"
def click_btn8():
    if label["text"] == "0":
        label["text"] = "8"
    else:
        label["text"] = label["text"] +"8"
def click_btn9():
    if label["text"] == "0":
        label["text"] = "9"
    else:
        label["text"] = label["text"] +"9"
def click_btn0():
    if label["text"] == "0":
        label["text"] = "0"
    else:
        label["text"] = label["text"] +"0"
    
def click_btnAc():
    global フラグ
    label["text"] = "0"
    buttonPlus["fg"] = "black"
    buttonMinus["fg"] = "black"
    buttonMulti["fg"] = "black"
    buttonDived["fg"] = "black"
    フラグ=0
    
def click_btnPlus():
    global box
    global hugo
    box = label["text"]
    label["text"] = "0"
    hugo = "+"
    buttonPlus["fg"] = "orange"
    buttonMinus["fg"] = "black"
    buttonMulti["fg"] = "black"
    buttonDived["fg"] = "black"
def click_btnMinus():
    global box
    global hugo
    box = label["text"]
    label["text"] = "0"
    hugo = "-"
    buttonPlus["fg"] = "black"
    buttonMinus["fg"] = "orange"
    buttonMulti["fg"] = "black"
    buttonDived["fg"] = "black"
def click_btnMulti():
    global box
    global hugo
    box = label["text"]
    label["text"] = "0"
    hugo = "×"
    buttonPlus["fg"] = "black"
    buttonMinus["fg"] = "black"
    buttonMulti["fg"] = "orange"
    buttonDived["fg"] = "black"
def click_btnDived():
    global box
    global hugo
    box = label["text"]
    label["text"] = "0"
    hugo = "÷"
    buttonPlus["fg"] = "black"
    buttonMinus["fg"] = "black"
    buttonMulti["fg"] = "black"
    buttonDived["fg"] = "orange"

def click_btnzeikomi():
    global box,フラグ
    if フラグ == 0:
        box = label["text"]
        box_Int1 = int(box)
        result = box_Int1 * 1.1
        label["text"] = int(result)
        フラグ=1

def click_btnEqual():
    box_Int1 = int(box)
    box2 = label["text"]
    box_Int2 = int(box2)
    
    if hugo == "+":
        result = box_Int1 + box_Int2
        label["text"] = result
    elif hugo == "-":
        result = box_Int1 - box_Int2
        label["text"] = result
    elif hugo == "×":
        result = box_Int1 * box_Int2
        label["text"] = result
    elif hugo == "÷":
        result = box_Int1 / box_Int2
        label["text"] = result      
    else:
        label["text"] = "Error"

ウィンドウ = tkinter.Tk()
ウィンドウ.title("計算機")
ウィンドウ.geometry("500x600")
ウィンドウ.resizable(False,False)
フラグ=0

#画像ファイル
計算機 = tkinter.PhotoImage(file="image\計算機.png")
ボタン0 = tkinter.PhotoImage(file="image\ボタン0.png")
ボタン1 = tkinter.PhotoImage(file="image\ボタン1.png")
ボタン2 = tkinter.PhotoImage(file="image\ボタン2.png")
ボタン3 = tkinter.PhotoImage(file="image\ボタン3.png")
ボタン4 = tkinter.PhotoImage(file="image\ボタン4.png")
ボタン5 = tkinter.PhotoImage(file="image\ボタン5.png")
ボタン6 = tkinter.PhotoImage(file="image\ボタン6.png")
ボタン7 = tkinter.PhotoImage(file="image\ボタン7.png")
ボタン8 = tkinter.PhotoImage(file="image\ボタン8.png")
ボタン9 = tkinter.PhotoImage(file="image\ボタン9.png")
ボタンAC = tkinter.PhotoImage(file="image\ボタンAC.png")
ボタン割る = tkinter.PhotoImage(file="image\ボタン÷.png")
ボタンプラス = tkinter.PhotoImage(file="image\ボタン+.png")
ボタン掛ける = tkinter.PhotoImage(file="image\ボタン×.png")
ボタンマイナス = tkinter.PhotoImage(file="image\ボタン-.png")
ボタンイコール = tkinter.PhotoImage(file="image\ボタン=.png")
ボタン税込 = tkinter.PhotoImage(file="image\ボタン税込.png")

キャンバス = tkinter.Canvas(ウィンドウ,width=900,height=700)
キャンバス.create_image(250,300,image=計算機)
キャンバス.place(x=0,y=0)

# ラベルやボタンの作成と配置
label = tkinter.Label(ウィンドウ, anchor="e",text="0",
                      background="white",font=("system",25,"bold"))
label.place(x=90, y=32, width=320, height=64)

button1 = tkinter.Button(ウィンドウ,text = "1",image=ボタン1,command=click_btn1)
button1.place(x=80, y=305, relwidth=0.16, relheight= 0.1)

button2 = tkinter.Button(ウィンドウ,text = "2",image=ボタン2,command=click_btn2)
button2.place(x=166, y=305, relwidth=0.16, relheight= 0.1)

button3 = tkinter.Button(ウィンドウ,text = "3",image=ボタン3,command=click_btn3)
button3.place(x=252, y=305, relwidth=0.16, relheight= 0.1)

buttonPlus = tkinter.Button(ウィンドウ,text = "+",image=ボタンプラス, command=click_btnPlus)
buttonPlus.place(x=338, y=305, relwidth=0.16, relheight= 0.1)

button4 = tkinter.Button(ウィンドウ,text = "4",image=ボタン4,command=click_btn4)
button4.place(x=80, y=242, relwidth=0.16, relheight= 0.1)

button5 = tkinter.Button(ウィンドウ,text = "5",image=ボタン5,command=click_btn5)
button5.place(x=166, y=242, relwidth=0.16, relheight= 0.1)

button6 = tkinter.Button(ウィンドウ,text = "6",image=ボタン6,command=click_btn6)
button6.place(x=252, y=242, relwidth=0.16, relheight= 0.1)

buttonMinus = tkinter.Button(ウィンドウ,text = "-",image=ボタンマイナス,command=click_btnMinus)
buttonMinus.place(x=338, y=242, relwidth=0.16, relheight= 0.1)

button7 = tkinter.Button(ウィンドウ,text = "7",image=ボタン7,command=click_btn7)
button7.place(x=80, y=178, relwidth=0.16, relheight= 0.1)

button8 = tkinter.Button(ウィンドウ,text = "8",image=ボタン8,command=click_btn8)
button8.place(x=166, y=178, relwidth=0.16, relheight= 0.1)

button9 = tkinter.Button(ウィンドウ,text = "9", image=ボタン9,command=click_btn9)
button9.place(x=252, y=178, relwidth=0.16, relheight= 0.1)

buttonMulti = tkinter.Button(ウィンドウ,text = "×",image=ボタン掛ける,command=click_btnMulti)
buttonMulti.place(x=337, y=178, relwidth=0.16, relheight= 0.1)

button0 = tkinter.Button(ウィンドウ,text = "0",image=ボタン0,command=click_btn0)
button0.place(x=80, y=369, relwidth=0.33, relheight= 0.1)

buttonEqual = tkinter.Button(ウィンドウ,text = "=",image=ボタンイコール,command=click_btnEqual)
buttonEqual.place(x=337, y=369, relwidth=0.16, relheight= 0.1)

buttonAc = tkinter.Button(ウィンドウ,text = "AC",image=ボタンAC,command=click_btnAc)
buttonAc.place(x=80, y=115, relwidth=0.17, relheight= 0.1)

buttonDived = tkinter.Button(ウィンドウ,text = "÷",image=ボタン割る,command=click_btnDived)
buttonDived.place(x=336, y=115, relwidth=0.16, relheight= 0.1)

buttonzeikomi = tkinter.Button(ウィンドウ,text = "税込み",image=ボタン税込,command=click_btnzeikomi)
buttonzeikomi.place(x=166, y=115, relwidth=0.335, relheight= 0.1)

ウィンドウ.mainloop()