-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
73 lines (62 loc) · 2.95 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
from tkinter import *
root = Tk()
root.title("Calculator")
# So that it becomes of fixed size
#root.resizable(0, 0)
# So that it remains on top of the screen
root.wm_attributes("-topmost", 1)
# Label
Label1 = Label(root, text = "Calculator app")
Label1.grid(row=0, columnspan=8)
# Variables
equal = ""
equation = StringVar()
calculation = Label(root, textvariable = equation)
equation.set("Enter expression")
calculation.grid(row=2, columnspan=8)
def btnPress(num):
global equal
equal = equal + str(num)
equation.set(equal)
def EqualPress():
global equal
total = str(eval(equal))
equation.set(total)
equal = ""
def ClearPress():
global equal
equal = ""
equation.set("")
Button0 = Button(root, text="0", command = lambda:btnPress(0), borderwidth=1, relief=SOLID)
Button0.grid(row = 6, column = 2, padx=10, pady=10)
Button1 = Button(root, text="1", command = lambda:btnPress(1), borderwidth=1, relief=SOLID)
Button1.grid(row = 3, column = 1, padx=10, pady=10)
Button2 = Button(root, text="2", command = lambda:btnPress(2), borderwidth=1, relief=SOLID)
Button2.grid(row = 3, column = 2, padx=10, pady=10)
Button3 = Button(root, text="3", command = lambda:btnPress(3), borderwidth=1, relief=SOLID)
Button3.grid(row = 3, column = 3, padx=10, pady=10)
Button4 = Button(root, text="4", command = lambda:btnPress(4), borderwidth=1, relief=SOLID)
Button4.grid(row = 4, column = 1, padx=10, pady=10)
Button5 = Button(root, text="5", command = lambda:btnPress(5), borderwidth=1, relief=SOLID)
Button5.grid(row = 4, column = 2, padx=10, pady=10)
Button6 = Button(root, text="6", command = lambda:btnPress(6), borderwidth=1, relief=SOLID)
Button6.grid(row = 4, column = 3, padx=10, pady=10)
Button7 = Button(root, text="7", command = lambda:btnPress(7), borderwidth=1, relief=SOLID)
Button7.grid(row = 5, column = 1, padx=10, pady=10)
Button8 = Button(root, text="8", command = lambda:btnPress(8), borderwidth=1, relief=SOLID)
Button8.grid(row = 5, column = 2, padx=10, pady=10)
Button9 = Button(root, text="9", command = lambda:btnPress(9), borderwidth=1, relief=SOLID)
Button9.grid(row = 5, column = 3, padx=10, pady=10)
Plus = Button(root, text="+", command = lambda:btnPress("+"), borderwidth=1, relief=SOLID)
Plus.grid(row = 3, column = 4, padx=10, pady=10)
Minus = Button(root, text="-", command = lambda:btnPress("-"), borderwidth=1, relief=SOLID)
Minus.grid(row = 4, column = 4, padx=10, pady=10)
Multiply = Button(root, text="*", command = lambda:btnPress("*"), borderwidth=1, relief=SOLID)
Multiply.grid(row = 5, column = 4, padx=10, pady=10)
Divide = Button(root, text="/", command = lambda:btnPress("/"), borderwidth=1, relief=SOLID)
Divide.grid(row = 6, column = 4, padx=10, pady=10)
Equal = Button(root, text="=", command = EqualPress, borderwidth=1, relief=SOLID)
Equal.grid(row=6, column=3, padx=10, pady=10)
Clear = Button(root, text="C", command = ClearPress, borderwidth=1, relief=SOLID)
Clear.grid(row = 6, column = 1, padx=10, pady=10)
root.mainloop()