파이썬으로 계산기를 만들어 보자

r5z Yoon·2022년 8월 24일
0

아이템 컬러를 지정해놓았는데 구현이 되지 않아 이것저것 찾아 봤는데도 이유를 알 수 없었다 버전이 맞지 않는 것도 같고...
아이템 컬러를 빼고 폰트 컬러를 블랙으로 고친 후 최종

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


def oprator_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  # 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(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,item in enumerate(items):

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


win.mainloop()

profile
_____ is a process </br> https://github.com/R5Z

0개의 댓글