-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
89 lines (61 loc) · 2.4 KB
/
calculator.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
import tkinter as tk
from tkinter import *
from tkinter import ttk
import math
expression = ""
def press(x):
global expression
expression = expression+str(x)
equation.set(expression)
def equals():
try:
global expression
total = str(eval(expression))
equation.set(total)
expression = ""
except:
equation.set("Error")
expression = ""
def clear():
global expression
expression = ""
equation.set(expression)
def undo():
global expression
expression = expression[0:len(expression)-1]
equation.set(expression)
if __name__ == '__main__':
# create a GUI window
gui = Tk()
# set the background colour of GUI window
gui.configure(background="light green")
# set the title of GUI window
gui.title("Simple Calculator")
# set the configuration of GUI window
gui.geometry("300x180")
equation = StringVar()
expression_field = Entry(gui, textvariable=equation)
expression_field.grid(columnspan=4, ipadx=70)
for i in range(1,10):
button = Button(gui, text = str(i), command= lambda i=i :press(i), height=1, width=3)
button.grid(column = (i-1)%3, row = math.floor((i-1)/3) + 1)
button0 = Button(gui, text = str(0), command= lambda :press(0), height=1, width=3)
button0.grid(column = 1, row = 4)
buttonPoint = Button(gui, text = ".", command= lambda :press("."), height=1, width=3)
buttonPoint.grid(column = 2 , row = 4)
buttonPlus = Button(gui, text = "+", command= lambda :press("+"), height=1, width=3)
buttonPlus.grid(column = 3 , row = 1)
buttonMinus = Button(gui, text = "-", command= lambda :press("-"), height=1, width=3)
buttonMinus.grid(column = 3 , row = 2)
buttonMult = Button(gui, text = "*", command= lambda :press("*"), height=1, width=3)
buttonMult.grid(column = 3 , row = 3)
buttonDiv = Button(gui, text = "/", command= lambda :press("/"), height=1, width=3)
buttonDiv.grid(column = 3 , row = 4)
buttonEqu = Button(gui, text = "=", command= lambda :equals())
buttonEqu.grid(column = 0 , row = 5, columnspan = 2, ipadx = 50)
buttonPlus = Button(gui, text = "BACK", command= lambda :undo(), height=1, width=3)
buttonPlus.grid(column = 2 , row = 5)
buttonPlus = Button(gui, text = "CLEAR", command= lambda :clear(), height=1, width=3)
buttonPlus.grid(column = 3, row = 5)
# start the GUI
gui.mainloop()