-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_cal.py
122 lines (95 loc) · 3.27 KB
/
python_cal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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 operator_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:
operator_click(value)
win = tk.Tk()
win.title('계산기')
disValue = 0
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 = btn = tk.Button(win,
text=item,
width=10,
height=5,
bg = color,
fg = 'white',
command = lambda cmd=item: button_click(cmd)
)
bt.grid(column=k, row = (i+1))
# btn = tk.Button(win, text='1', width=10, height=5).grid(column=0, row = 1)
# btn = tk.Button(win, text='2', width=10, height=5).grid(column=1, row = 1)
# btn = tk.Button(win, text='3', width=10, height=5).grid(column=2, row = 1)
# btn = tk.Button(win, text='4', width=10, height=5).grid(column=3, row = 1)
# btn = tk.Button(win, text='5', width=10, height=5).grid(column=0, row = 2)
# btn = tk.Button(win, text='6', width=10, height=5).grid(column=1, row = 2)
# btn = tk.Button(win, text='7', width=10, height=5).grid(column=2, row = 2)
# btn = tk.Button(win, text='8', width=10, height=5).grid(column=3, row = 2)
# btn = tk.Button(win, text='9', width=10, height=5).grid(column=0, row = 3)
# btn = tk.Button(win, text='0', width=10, height=5).grid(column=1, row = 3)
# btn = tk.Button(win, text='+', width=10, height=5).grid(column=2, row = 3)
# btn = tk.Button(win, text='-', width=10, height=5).grid(column=3, row = 3)
# btn = tk.Button(win, text='/', width=10, height=5).grid(column=0, row = 4)
# btn = tk.Button(win, text='*', width=10, height=5).grid(column=1, row = 4)
# btn = tk.Button(win, text='C', width=10, height=5).grid(column=2, row = 4)
# btn = tk.Button(win, text='=', width=10, height=5).grid(column=3, row = 4)
win.mainloop()