본문 바로가기
코딩관련

GUI계산기(python)

by Hawie 2022. 8. 21.
from ast import operator
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, operator, stoValue, oppre
stoValue = 0
oppre = 0
disValue = 0
str_value.set(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= 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):

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

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





win.mainloop()

#첫작품

역시 처음 해보는거라 시행 착오가 많았다...

스펠링 하나 틀려서 찾는데 오래걸린게 문제 인듯 하다.

다음 부터는 더 정확한 타이핑을 해야겠다.

완성화면

'코딩관련' 카테고리의 다른 글

기억력 테스트 게임  (0) 2022.08.27
추억의 오락실 게임 만들기.  (0) 2022.08.21

댓글