[calculator.py]
import tkinter as tk
from turtle import st
# GUI 설계 후
# 내부 logic 설계
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 operator_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 = 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 clear() :
global disvalue, stovalue, opPre
stovalue = 0
opPre = 0
disvalue = 0
str_value.set(disvalue)
def btn_click(value) :
print(value)
try :
value = int(value)
number_click(value)
except :
operator_click(value)
win = tk.Tk()
win.title('calculaor')
str_value = tk.StringVar()
str_value.set(str(disvalue))
dis = tk.Entry(win, textvariable = str_value, justify = 'right', bg = 'white', fg = 'red').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 = 'green'
bt =btn = tk.Button(win,
text=item,
width=10,
height=5,
bg = color,
fg='white',
command = lambda cmd=item : btn_click(cmd)
)
bt.grid(column=k, row=(i+1))
# btn = tk.Button(win, text='1', width=10, height=5)
# btn.grid(column=0, row=1)
# # btn = tk.Button(win, text='1', width=10, height=5).grid(column=0, row=1) 과 동일하게 표현 할 수 있음.
win.mainloop()'source Code > Python' 카테고리의 다른 글
| Python | py_basic_week1 (0) | 2022.09.02 |
|---|---|
| Python | py_basic_week1 (0) | 2022.09.01 |
| Python | poo_game.py (0) | 2022.09.01 |
| Python | memory_game.py (0) | 2022.08.26 |
| Python | weapon_game.py (0) | 2022.08.25 |