-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit
More file actions
75 lines (73 loc) · 3.2 KB
/
edit
File metadata and controls
75 lines (73 loc) · 3.2 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from tkinter import *
import tkinter.messagebox
import math
class calc:
def solve(self):
self.aa=self.e.get()
try:
val=eval(self.aa)
self.e.delete(0, END)
self.e.insert(0, val)
except SyntaxError or NameError:
tkinter.messagebox.showerror('Result', 'Error')
def clearelement(self):
self.text=self.e.get()[:-1]
self.e.delete(0,END)
self.e.insert(0,self.text)
def clearfield(self):
self.e.delete(0,END)
def action(self,argi):
self.e.insert(END,argi)
def value(self):
aa=self.e.get()
print(aa)
def squareroot(self):
self.solve()
try:
self.sol=eval(self.aa)
except SyntaxError or NameError:
tkinter.messagebox.showerror('Result','Error')
else:
self.sq=math.sqrt(self.sol)
self.e.delete(0,END)
self.e.insert(0,self.sq)
def square(self):
self.solve()
try:
self.b=eval(self.aa)
except SyntaxError or NameError:
tkinter.messagebox.showerror('Result','Error')
else:
self.root=math.pow(self.b,2)
self.e.delete(0,END)
self.e.insert(0,self.root)
def __init__(self, master):
self.e = Entry(master)
self.e.grid(row=0, columnspan=20)
self.e.focus_set()
bt1=Button(master,text="=",width=12,command=lambda:self.solve()).grid(row=4, column=3,columnspan=4)
master.bind('<Return>',lambda x :self.solve())
bt2=Button(master,text='AC',width=3,command=lambda:self.clearfield()).grid(row=1, column=4)
bt3=Button(master,text='C',width=3,command=lambda:self.clearelement()).grid(row=1, column=5)
bt4=Button(master,text="+",width=3,command=lambda:self.action('+')).grid(row=4, column=2)
bt5=Button(master,text="x",width=3,command=lambda:self.action('*')).grid(row=2, column=3)
bt6=Button(master,text="-",width=3,command=lambda:self.action('-')).grid(row=3, column=3)
bt7=Button(master,text="÷",width=3,command=lambda:self.action('/')).grid(row=1, column=3)
bt9=Button(master,text="7",width=3,command=lambda:self.action('7')).grid(row=1, column=0)
bt10=Button(master,text="8",width=3,command=lambda:self.action('8')).grid(row=1, column=1)
bt11=Button(master,text="9",width=3,command=lambda:self.action('9')).grid(row=1, column=2)
bt12=Button(master,text="4",width=3,command=lambda:self.action('4')).grid(row=2, column=0)
bt13=Button(master,text="5",width=3,command=lambda:self.action('5')).grid(row=2, column=1)
bt14=Button(master,text="6",width=3,command=lambda:self.action('6')).grid(row=2, column=2)
bt15=Button(master,text="1",width=3,command=lambda:self.action('1')).grid(row=3, column=0)
bt16=Button(master,text="2",width=3,command=lambda:self.action('2')).grid(row=3, column=1)
bt17=Button(master,text="3",width=3,command=lambda:self.action('3')).grid(row=3, column=2)
bt18=Button(master,text="0",width=3,command=lambda:self.action('0')).grid(row=4, column=0)
bt19=Button(master,text=".",width=3,command=lambda:self.action('.')).grid(row=4, column=1)
bt20=Button(master,text="(",width=3,command=lambda:self.action('(')).grid(row=2, column=4)
bt21=Button(master,text=")",width=3,command=lambda:self.action(')')).grid(row=2, column=5)
bt22=Button(master,text="√",width=3,command=lambda:self.squareroot()).grid(row=3, column=4)
bt23=Button(master,text="x²",width=3,command=lambda:self.square()).grid(row=3, column=5)
root=Tk()
obj=calc(root)
root.mainloop()