-
Notifications
You must be signed in to change notification settings - Fork 1
/
tkinter-calc.py
171 lines (132 loc) · 6.16 KB
/
tkinter-calc.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from tkinter import *
master = Tk()
master.title("Calculator")
master.geometry("500x650")
master.resizable(False, False)
def one():
global expression
expression += "1"
expressionLabel = Label(master, text=expression, width=50, font="Arial 24")
expressionLabel.place(relx=0.5, rely=0.03, anchor=CENTER)
def two():
global expression
expression += "2"
expressionLabel = Label(master, text=expression, width=50, font="Arial 24")
expressionLabel.place(relx=0.5, rely=0.03, anchor=CENTER)
def three():
global expression
expression += "3"
expressionLabel = Label(master, text=expression, width=50, font="Arial 24")
expressionLabel.place(relx=0.5, rely=0.03, anchor=CENTER)
def four():
global expression
expression += "4"
expressionLabel = Label(master, text=expression, width=50, font="Arial 24")
expressionLabel.place(relx=0.5, rely=0.03, anchor=CENTER)
def five():
global expression
expression += "5"
expressionLabel = Label(master, text=expression, width=50, font="Arial 24")
expressionLabel.place(relx=0.5, rely=0.03, anchor=CENTER)
def six():
global expression
expression += "6"
expressionLabel = Label(master, text=expression, width=50, font="Arial 24")
expressionLabel.place(relx=0.5, rely=0.03, anchor=CENTER)
def seven():
global expression
expression += "7"
expressionLabel = Label(master, text=expression, width=50, font="Arial 24")
expressionLabel.place(relx=0.5, rely=0.03, anchor=CENTER)
def eight():
global expression
expression += "8"
expressionLabel = Label(master, text=expression, width=50, font="Arial 24")
expressionLabel.place(relx=0.5, rely=0.03, anchor=CENTER)
def nine():
global expression
expression += "9"
expressionLabel = Label(master, text=expression, width=50, font="Arial 24")
expressionLabel.place(relx=0.5, rely=0.03, anchor=CENTER)
def add():
global expression
expression += "+"
expressionLabel = Label(master, text=expression, width=50, font="Arial 24")
expressionLabel.place(relx=0.5, rely=0.03, anchor=CENTER)
def subtract():
global expression
expression += "-"
expressionLabel = Label(master, text=expression, width=50, font="Arial 24")
expressionLabel.place(relx=0.5, rely=0.03, anchor=CENTER)
def multiply():
global expression
expression += "*"
expressionLabel = Label(master, text=expression, width=50, font="Arial 24")
expressionLabel.place(relx=0.5, rely=0.03, anchor=CENTER)
def divide():
global expression
expression += "÷"
expressionLabel = Label(master, text=expression, width=50, font="Arial 24")
expressionLabel.place(relx=0.5, rely=0.03, anchor=CENTER)
def power():
global expression
expression += "^"
expressionLabel = Label(master, text=expression, width=50, font="Arial 24")
expressionLabel.place(relx=0.5, rely=0.03, anchor=CENTER)
def evalExpress():
global expression
global answer
expressToEval = expression.replace("^", "**")
expressToEval = expressToEval.replace("÷", "/")
expression += " = "
answer = Label(master, text=eval(expressToEval), width=50, font="Arial 24")
answer.place(relx=0.5, rely=0.1, anchor=CENTER)
expressionLabel = Label(master, text=expression, width=50, font="Arial 24")
expressionLabel.place(relx=0.5, rely=0.03, anchor=CENTER)
def clear():
global expression
expression = ""
expressionLabel = Label(master, text=expression, width=50, font="Arial 24")
expressionLabel.place(relx=0.5, rely=0.03, anchor=CENTER)
answer = Label(master, text="", width=50, font="Arial 24")
answer.place(relx=0.5, rely=0.1, anchor=CENTER)
def runCalc():
global expression
expression = ""
expressionLabel = Label(master, text=expression, width=50, font="Arial 24")
expressionLabel.place(relx=0.5, rely=0.03, anchor=CENTER)
# Buttons
button1 = Button(master, text="1", font="Courier 20 bold", width=7, height=3, command=one)
button1.place(relx=0, rely=0.125)
button2 = Button(master, text="2", font="Courier 20 bold", width=7, height=3, command=two)
button2.place(relx=0.25, rely=0.125)
button3 = Button(master, text="3", font="Courier 20 bold", width=7, height=3, command=three)
button3.place(relx=0.5, rely=0.125)
button4 = Button(master, text="4", font="Courier 20 bold", width=7, height=3, command=four)
button4.place(relx=0, rely=0.3125)
button5 = Button(master, text="5", font="Courier 20 bold", width=7, height=3, command=five)
button5.place(relx=0.25, rely=0.3125)
button6 = Button(master, text="6", font="Courier 20 bold", width=7, height=3, command=six)
button6.place(relx=0.5, rely=0.3125)
button7 = Button(master, text="7", font="Courier 20 bold", width=7, height=3, command=seven)
button7.place(relx=0, rely=0.5)
button8 = Button(master, text="8", font="Courier 20 bold", width=7, height=3, command=eight)
button8.place(relx=0.25, rely=0.5)
button9 = Button(master, text="9", font="Courier 20 bold", width=7, height=3, command=nine)
button9.place(relx=0.5, rely=0.5)
buttonAdd = Button(master, text="+", font="Courier 20 bold", width=7, height=3, command=add)
buttonAdd.place(relx=0.75, rely=0.125)
buttonSubtract = Button(master, text="-", font="Courier 20 bold", width=7, height=3, command=subtract)
buttonSubtract.place(relx=0.75, rely=0.3125)
buttonMultiply = Button(master, text="*", font="Courier 20 bold", width=7, height=3, command=multiply)
buttonMultiply.place(relx=0.75, rely=0.5)
buttonDivide = Button(master, text="÷", font="Courier 20 bold", width=7, height=3, command=divide)
buttonDivide.place(relx=0.75, rely=0.6875)
buttonPower = Button(master, text="^", font="Courier 20 bold", width=7, height=3, command=power)
buttonPower.place(relx=0.5, rely=0.6875)
buttonClear = Button(master, text="C", font="Courier 20 bold", width=7, height=3, command=clear)
buttonClear.place(relx=0.25, rely=0.6875)
buttonEquals = Button(master, text="=", font="Courier 20 bold", width=7, height=3, command=evalExpress)
buttonEquals.place(relx=0, rely=0.6875)
mainloop()
runCalc()