본문 바로가기
코딩/파이썬

tkinter 라이브러리를 이용하여 간단한 계산기 프로그램 만들기

by yenua 2023. 5. 2.
반응형

계산기.py
0.00MB

from tkinter import *

a=''

def cal(value):
    global a
    a+=str(value)
    entry1.delete(0, END)
    entry1.insert (0, a)

def clear():
    global a
    a=''
    entry1.delete(0, END)

def result():
    global a
    entry1.delete(0, END)
    entry1.insert (0, eval(a))
    
window=Tk()
window.title("계산기")

entry1 = Entry(window, width=31, bg ="yellow")
entry1.grid(row=0, column=0, columnspan=5)

Button(window, width=5, text='7', command=lambda: cal(7)).grid(row=1, column=0)
Button(window, width=5, text='8', command=lambda: cal(8)).grid(row=1, column=1)
Button(window, width=5, text='9', command=lambda: cal(9)).grid(row=1, column=2)
Button(window, width=5, text='/', command=lambda: cal('/')).grid(row=1, column=3)
Button(window, width=5, text='C', command=lambda: clear()).grid(row=1, column=4)
Button(window, width=5, text='4', command=lambda: cal(4)).grid(row=2, column=0)
Button(window, width=5, text='5', command=lambda: cal(5)).grid(row=2, column=1)
Button(window, width=5, text='6', command=lambda: cal(6)).grid(row=2, column=2)
Button(window, width=5, text='*', command=lambda: cal('*')).grid(row=2, column=3)
Button(window, width=5, text=' ').grid(row=2, column=4)
Button(window, width=5, text='1', command=lambda: cal(1)).grid(row=3, column=0)
Button(window, width=5, text='2', command=lambda: cal(2)).grid(row=3, column=1)
Button(window, width=5, text='3', command=lambda: cal(3)).grid(row=3, column=2)
Button(window, width=5, text='-', command=lambda: cal('-')).grid(row=3, column=3)
Button(window, width=5, text=' ').grid(row=3, column=4)
Button(window, width=5, text='0', command=lambda: cal(0)).grid(row=4, column=0)
Button(window, width=5, text='.', command=lambda: cal('.')).grid(row=4, column=1)
Button(window, width=5, text='=', command=lambda: result()).grid(row=4, column=2)
Button(window, width=5, text='+', command=lambda: cal('+')).grid(row=4, column=3)
Button(window, width=5, text=' ').grid(row=4, column=4)

window.mainloop()
반응형