0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Python】Tkinterで計算機作成してみた!

Last updated at Posted at 2023-06-18

計算機プログラム製造

今日の記事は計算機プログラムの成果物をキータに投稿します。

画像

2023-06-18 (3).png

機能

グリッドのボタンをクリックすると数字で計算をすることが出来ます。
rowやcolumnを使用して位置を調整しています。
そこが微妙に難しかったです。
演算子を押下した際には関数を呼び出して計算しています。

以下にプログラムのソースコードを連携します。

ソースコード

from tkinter import *
from turtle import bgcolor
from sympy import Lambda
root = Tk()
root.title('計算機')

e = Entry(root, width=35, borderwidth=5)
e.grid(row=0, column=0, columnspan=3, padx=20, pady=10)

def button_click(number):
    current = e.get()
    e.delete(0, END) # クリックをしたら前のデータを消す。
    e.insert(0,str(current)+ str (number)) # 複数入力された場合には繋げる。
def button_clear():
    e.delete(0, END) # 全ての数字を消す。
def button_add():
    first_number = e.get()
    global f_num
    global math
    math = 'addition'
    f_num = int(first_number)
    e.delete(0, END)
    
def button_equal():
    """
    クリックされた場合のmathの値を見て計算を行う。
    """
    second_number = e.get() # 二番目に入力した値を受け取る。
    e.delete(0, END) # 削除
    if math == 'addition': # mathの中身を見て何の計算を行うのか判断をしている。
        e.insert(0, f_num + int(second_number)) # 足し算をして結果を表示
    if math == 'subtract':
        e.insert(0, f_num - int(second_number)) # 引き算をして結果を表示
    if math == 'multiply':
        e.insert(0, f_num * int(second_number)) # 掛け算をして結果を表示
    if math == 'divide':
        e.insert(0, f_num // int(second_number)) # 割り算をして結果を表示(小数点は切り捨て)
    if math == 'multiplier':
        e.insert(0, f_num ** int(second_number))
def button_subtract():
    """
    -をクリックした時
    """
    first_number = e.get()
    global f_num
    global math
    math = 'subtract'
    f_num = int(first_number)
    e.delete(0, END)
def button_multiply():
    """
    *をクリックした時
    """
    first_number = e.get()
    global f_num
    global math
    math = 'multiply'
    f_num = int(first_number)
    e.delete(0, END)
def button_divide():
    """
    /をクリックした時
    """
    first_number = e.get()
    global f_num
    global math
    math = 'divide'
    f_num = int(first_number)
    e.delete(0, END)
    
# ボタンのプロパティ
button_1 = Button(root, text= '1', padx =40, pady =20, bg='#00ff8c',activebackground='#00ff8c',command=lambda: button_click(1)) # 数字を渡す。lambdaを使用する。関数の定義
button_2 = Button(root, text= '2', padx =40, pady =20, bg='#00ff8c',activebackground='#00ff8c',command=lambda: button_click(2))
button_3 = Button(root, text= '3', padx =41, pady =20, bg='#00ff8c',activebackground='#00ff8c',command=lambda: button_click(3))
button_4 = Button(root, text= '4', padx =40, pady =20, bg='#00ff8c',activebackground='#00ff8c',command=lambda: button_click(4))
button_5 = Button(root, text= '5', padx =40, pady =20, bg='#00ff8c',activebackground='#00ff8c',command=lambda: button_click(5))
button_6 = Button(root, text= '6', padx =41, pady =20, bg='#00ff8c',activebackground='#00ff8c',command=lambda: button_click(6))
button_7 = Button(root, text= '7', padx =40, pady =20, bg='#00ff8c',activebackground='#00ff8c',command=lambda: button_click(7))
button_8 = Button(root, text= '8', padx =40, pady =20, bg='#00ff8c',activebackground='#00ff8c',command=lambda: button_click(8))
button_9 = Button(root, text= '9', padx =41, pady =20, bg='#00ff8c',activebackground='#00ff8c',command=lambda: button_click(9))
button_0 = Button(root, text= '0', padx =40, pady =20, bg='#00ff8c',activebackground='#00ff8c',command=lambda: button_click(0))

button_add = Button(root, text= '+', padx =39, pady =20, bg='#00ff8c',activebackground='#00ff8c',command=button_add)
button_equal = Button(root, text= '=', padx =87, pady =20, bg='#00ff8c',activebackground='#00ff8c',command=button_equal)
button_clear = Button(root, text= 'Clear', padx =78, pady =20, bg='#00ff8c',activebackground='#00ff8c',command=button_clear)

button_subtract = Button(root, text= '-', padx =40.5, pady =20, bg='#00ff8c',activebackground='#00ff8c',command=button_subtract)
button_multiply = Button(root, text= '*', padx =40.5, pady =20, bg='#00ff8c',activebackground='#00ff8c',command=button_multiply)
button_divide = Button(root, text= '/', padx =40, pady =20, bg='#00ff8c',activebackground='#00ff8c',command=button_divide)

# グリッドの形にする。
# 場所の設定
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)

button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)

button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)

button_0.grid(row=4, column=0)
button_clear.grid(row=4, column=1, columnspan=2)
button_add.grid(row=5,column=0)
button_equal.grid(row=5, column=1, columnspan=2)

button_subtract.grid(row=6, column=0)
button_multiply.grid(row=6, column=1)
button_divide.grid(row=6, column=2)

root.mainloop() # 計算機アプリを表示
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?