-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.py
117 lines (98 loc) · 4.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
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
# import tkinter for gui
from tkinter import *
# get number function
def btn(number):
global operater
operater = operater + str(number)
textInput.set(operater)
# clear function
def clear():
global operater
operater = ''
textInput.set('')
Display.insert(0, 'Start Calculating..')
# finding the result
def equal():
global operater
sumup = float(eval(operater))
textInput.set(sumup)
operater = ''
root = Tk()
root.title("Calculator")
operater = ''
textInput = StringVar(value='Start calculating')
######################## Display Screen ########################
Display = Entry(root, font=('araial', 30, 'bold'), fg='black', bg='white', justify='right', bd='20',
textvariable=textInput)
Display.grid(columnspan='4')
######################## Screen ########################
# Buttons
###################################### row 1
btn7 = Button(root, padx='40', pady='15', bd='8', fg='white', font='arial,30,bold', text='7', bg='green',
command=lambda: btn(7)).grid(row=1,
column=0)
btn8 = Button(root, padx='40', pady='15', bd='8', fg='white', font='arial,30,bold', text='8', bg='green',
command=lambda: btn(8)).grid(row=1,
column=1)
btn9 = Button(root, padx='40', pady='15', bd='8', fg='white', font='arial,30,bold', text='9', bg='green',
command=lambda: btn(9)).grid(row=1,
column=2)
btnC = Button(root, padx='40', pady='15', bd='8', fg='white', font='arial,30,bold', text='C', bg='red',
command=lambda: clear()).grid(row=1,
column=3)
###################################### row 2
btn4 = Button(root, padx='40', pady='15', bd='8', fg='white', font='arial,30,bold', text='4', bg='green',
command=lambda: btn(4)).grid(row=2,
column=0)
btn5 = Button(root, padx='40', pady='15', bd='8', fg='white', font='arial,30,bold', text='5', bg='green',
command=lambda: btn(5)).grid(row=2,
column=1)
btn6 = Button(root, padx='40', pady='15', bd='8', fg='white', font='arial,30,bold', text='6', bg='green',
command=lambda: btn(6)).grid(row=2,
column=2)
btnPlus = Button(root, padx='40', pady='15', bd='8', fg='white', font='arial,30,bold', text='+', bg='orange',
command=lambda: btn('+')).grid(
row=2,
column=3)
##################################### row 3
btn1 = Button(root, padx='40', pady='15', bd='8', fg='white', font='arial,30,bold', text='1', bg='green',
command=lambda: btn(1)).grid(row=3,
column=0)
btn2 = Button(root, padx='40', pady='15', bd='8', fg='white', font='arial,30,bold', text='2', bg='green',
command=lambda: btn(2)).grid(row=3,
column=1)
btn3 = Button(root, padx='40', pady='15', bd='8', fg='white', font='arial,30,bold', text='3', bg='green',
command=lambda: btn(3)).grid(row=3,
column=2)
btnMinus = Button(root, padx='40', pady='15', bd='8', fg='white', font='arial,30,bold', text='-', bg='orange',
command=lambda: btn('-')).grid(
row=3,
column=3)
#################################### row 4
btnZero = Button(root, padx='40', pady='15', bd='8', fg='white', font='arial,30,bold', text='0', bg='green',
command=lambda: btn(0)).grid(row=4,
column=0)
btnDot = Button(root, padx='40', pady='15', bd='8', fg='white', font='arial,30,bold', text='.', bg='green',
command=lambda: btn('.')).grid(row=4,
column=1)
btnDivision = Button(root, padx='43', pady='15', bd='8', fg='white', font='arial,30,bold', text='/', bg='orange',
command=lambda: btn('/')).grid(
row=4,
column=2)
btnMultiply = Button(root, padx='40', pady='15', bd='8', fg='white', font='arial,30,bold', text='X', bg='orange',
command=lambda: btn('*')).grid(
row=4,
column=3)
#################################### row 5
btnEQ = Button(root, padx='100', pady='15', bd='8', fg='white', font='arial,30,bold', text='=', bg='blue',
command=lambda: equal()).grid(row=5,
column=0,
columnspan='2')
btnStBracket = Button(root, padx='40', pady='15', bd='8', fg='white', font='arial,30,bold', text='(', bg='orange',
command=lambda: btn('(')).grid(
row=5, column=2)
btnClBracket = Button(root, padx='40', pady='15', bd='8', fg='white', font='arial,30,bold', text=')', bg='orange',
command=lambda: btn(")")).grid(
row=5, column=3)
# loop the root for ui keep running.
root.mainloop()