-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator_codesoft.py
More file actions
61 lines (40 loc) · 1.3 KB
/
Copy pathcalculator_codesoft.py
File metadata and controls
61 lines (40 loc) · 1.3 KB
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
import tkinter as tk
class calculator:
def __init__(self,root):
self.root=root
self.root.title("Calcultor")
self.entry=tk.Entry(root,width=30,borderwidth=5)
self.entry.grid(row=0,column=0,columnspan=4)
self.calci_buttons()
def calci_buttons(self):
buttons_char=[
'1','2','3','+',
'4','5','6','-',
'7','8','9','*',
'0','=','%','/',
]
r=1
c=0
for text in buttons_char:
tk.Button(self.root, text=text, padx=20, pady=20, command=lambda t=text: self.click(t)).grid(row=r, column=c)
c += 1
if c > 3:
c= 0
r+= 1
def click(self,text):
if text=='=':
try:
result = eval(self.entry.get())
self.entry.delete(0, tk.END)
self.entry.insert(0, str(result))
except:
self.entry.delete(0, tk.END)
self.entry.insert(0, "Error")
else:
text_now=self.entry.get()
self.entry.delete(0,tk.END)
self.entry.insert(0,text_now+text)
if __name__ == "__main__":
root = tk.Tk()
calci = calculator(root)
root.mainloop()