일상정보
스파르타 내일배움캠프 AI 사전과제 계산기
꿀떡,
2022. 8. 23. 04:21
728x90
300x250
사전과제2
파이썬으로 계산기 만들기
import tkinter as tk
disValue = 0
operator = {'+':1,'-':2,'/':3,'*':4,'C':5,'=':6}
stovalue = 0
opPre = 0
def number_click(value):
# print('숫자',value)
global disValue
disValue = (disValue*10) + value
str_value.set(disValue)
def clear():
global disValue, stoValue, opPre
stoValue = 0
opPre = 0
disValue = 0
str_value.set(str(disValue))
def oprator_click(value):
# print('명령', value)
global disValue, operator, stoValue, opPre
op = operator[value]
if op == 5:
clear()
elif disValue == 0:
opPre = 0
elif opPre == 0:
opPre = 0
stoValue = disValue
disValue = 0
str_value.set(str(disValue))
elif op == 6:
if opPre == 1: # +
disValue = stoValue + disValue
if opPre == 2: # -
disValue = stoValue - disValue
if opPre == 3: # /
disValue = stoValue / disValue
if opPre == 4: # *
disValue = stoValue * disValue
str_value.set(str(disValue))
disValue = 0
stoValue = 0
opPre = 0
else:
clear()
def button_click(value):
# print(value)
try:
value = int(value)
number_click(value)
except:
oprator_click(value)
win = tk.Tk()
win.title('계산시')
str_value = tk.StringVar()
str_value.set(str(disValue))
dis = tk.Entry(win, textvariable=str_value, justify='right', bg = 'white', fg = 'red')
dis.grid(column=0, row=0, columnspan=4, ipadx=80, ipady=30)
CalItem =[['1','2','3','4'],
['5', '6', '7', '8'],
['9', '0', '+', '-'],
['/', '*', 'C', '=']]
for i,items in enumerate(CalItem):
for k,itme in enumerate(items):
try:
color = int(itme)
color = 'black'
except:
color = 'green'
bt = tk.Button(win,
text=itme,
width=10,
height=5,
bg = color,
fg = 'white',
command=lambda cmd=itme: button_click(cmd)
)
bt.grid(column=k, row=(i+1))
win.mainloop()
300x250