반응형
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()
반응형
'코딩 > 파이썬' 카테고리의 다른 글
plt.savefig 함수에서의 must be real number, not str 오류 발생 (0) | 2024.01.09 |
---|---|
[업무 자동화] 파이썬 UI를 통해 엑셀에 기록하는 프로그램 (0) | 2023.06.21 |
[python] OpenCV medianBlur() 'Assertion failed' 오류 해결 방법 (0) | 2022.09.14 |
[파이썬] 파이썬 하위 폴더의 파일에서 클래스 import 하는 방법 (0) | 2022.06.01 |
[numpy] 'numpy.ndarray' object is not callable 오류 해결하기 (0) | 2022.03.20 |