스파르타코딩클럽 계산기 숙제(연습)

김태인·2022년 4월 12일
0

코드 자체를 이해하려고 많은 노력을 기울이며 듣고 또 듣고 하였으나

익숙하지 않은것들이 너무많아

사실상 거의 따라하기 형식이 되어버렸다...

하지만 어찌 첫술에 배부르랴

언젠간 코드를 보고 이해하는 날이 오길 간절히 바라며 숙제제출~!

참고 유튜브 : https://www.youtube.com/watch?v=NT7HpqRtUP0&t=257s

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
    opPre = 0
    stoValue = 0
    disValue = 0
    str_value.set(disValue)

def operater_click(value):
    # print('명령',value)
    global disValue, operator, stoValue, opPre
    op = operator[value]
    if op == 5: # C
        clear()
    elif disValue == 0:
        opPre = 0
    elif opPre == 0:
        opPre = op
        stoValue = disValue
        disValue = 0
        str_value.set(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(disValue)
        disValue = 0
        stoValue = 0
        opPre = 0
    else:
        clear()


def button_click(value):
    # print(value)
    try:
        value = int(value)
        number_click(value)
    except:
        operater_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,item in enumerate(items):

        try:
            color = int(item)
            color = 'black'
        except:
            color = 'red'

        bt = tk.Button(win,
                   text=item,
                   width=10,
                   height=5,
                   fg='black',
                   bg='white',
                   command=lambda cmd=item: button_click(cmd)
                   )
        bt.grid(column=k, row=(i+1))

win.mainloop()
profile
코딩이 취미가 되는 그날까지

0개의 댓글

관련 채용 정보