1
+ # Importing the library
2
+ from future .moves import tkinter
3
+ import math
4
+
5
+
6
+ # To provide functionalities
7
+ def click (val ):
8
+ e = entry .get () # getting the value
9
+ ans = " "
10
+
11
+ try :
12
+ # To clear the last inserted text
13
+ if val == "C" :
14
+ e = e [0 :len (e ) - 1 ] # deleting the last entered value
15
+ entry .delete (0 , "end" )
16
+ entry .insert (0 , e )
17
+ return
18
+
19
+ # To delete everything
20
+ elif val == "CE" :
21
+ entry .delete (0 , "end" )
22
+
23
+ # Square root
24
+ elif val == "√" :
25
+ ans = math .sqrt (eval (e ))
26
+
27
+ # pi value
28
+ elif val == "π" :
29
+ ans = math .pi
30
+
31
+ # cos value
32
+ elif val == "cosθ" :
33
+ ans = math .cos (math .radians (eval (e )))
34
+
35
+ # sin value
36
+ elif val == "sinθ" :
37
+ ans = math .sin (math .radians (eval (e )))
38
+
39
+ # tan Value
40
+ elif val == "tanθ" :
41
+ ans = math .tan (math .radians (eval (e )))
42
+
43
+ # 2π value
44
+ elif val == "2π" :
45
+ ans = 2 * math .pi
46
+
47
+ # cosh value
48
+ elif val == "cosh" :
49
+ ans = math .cosh (eval (e ))
50
+
51
+ # sinh value
52
+ elif val == "sinh" :
53
+ ans = math .sinh (eval (e ))
54
+
55
+ # tanh value
56
+ elif val == "tanh" :
57
+ ans = math .tanh (eval (e ))
58
+
59
+ # cube root value
60
+ elif val == chr (8731 ):
61
+ ans = eval (e ) ** (1 / 3 )
62
+
63
+ # x to the power y
64
+ elif val == "x\u02b8 " :
65
+ entry .insert ("end" , "**" )
66
+ return
67
+
68
+ # cube value
69
+ elif val == "x\u00B3 " :
70
+ ans = eval (e ) ** 3
71
+
72
+ # square value
73
+ elif val == "x\u00B2 " :
74
+ ans = eval (e ) ** 2
75
+
76
+ # ln value
77
+ elif val == "ln" :
78
+ ans = math .log2 (eval (e ))
79
+
80
+ # deg value
81
+ elif val == "deg" :
82
+ ans = math .degrees (eval (e ))
83
+
84
+ # radian value
85
+ elif val == "rad" :
86
+ ans = math .radians (eval (e ))
87
+
88
+ # e value
89
+ elif val == "e" :
90
+ ans = math .e
91
+
92
+ # log10 value
93
+ elif val == "log10" :
94
+ ans = math .log10 (eval (e ))
95
+
96
+ # factorial value
97
+ elif val == "x!" :
98
+ ans = math .factorial (eval (e ))
99
+
100
+ # division operator
101
+ elif val == chr (247 ):
102
+ entry .insert ("end" , "/" )
103
+ return
104
+
105
+ elif val == "=" :
106
+ ans = eval (e )
107
+
108
+ else :
109
+ entry .insert ("end" , val )
110
+ return
111
+
112
+ entry .delete (0 , "end" )
113
+ entry .insert (0 , ans )
114
+
115
+ except SyntaxError :
116
+ pass
117
+
118
+
119
+ # Created the object
120
+ root = tkinter .Tk ()
121
+
122
+ # Setting the title and geometry
123
+ root .title ("Scientific Calculator" )
124
+ root .geometry ("680x486+100+100" )
125
+
126
+ # Setting the background color
127
+ root .config (bg = "black" )
128
+
129
+
130
+ # Entry field
131
+ entry = tkinter .Entry (root , font = ("arial" , 20 , "bold" ), bg = "black" , fg = "white" , bd = 10 , width = 30 )
132
+ entry .grid (row = 0 , column = 0 , columnspan = 8 )
133
+
134
+ # buttons list
135
+ button_list = ["C" , "CE" , "√" , "+" , "π" , "cosθ" , "tanθ" , "sinθ" , "1" , "2" , "3" , "-" , "2π" , "cosh" , "tanh" , "sinh" ,
136
+ "4" , "5" , "6" , "*" , chr (8731 ), "x\u02b8 " , "x\u00B3 " , "x\u00B2 " , "7" , "8" , "9" , chr (247 ), "ln" , "deg" ,
137
+ "rad" , "e" , "0" , "." , "%" , "=" , "log10" , "(" , ")" , "x!" ]
138
+ r = 1
139
+ c = 0
140
+ # Loop to get the buttons on window
141
+ for i in button_list :
142
+ # Buttons
143
+ button = tkinter .Button (root , width = 5 , height = 2 , bd = 2 , text = i , bg = "black" , fg = "white" ,
144
+ font = ("arial" , 18 , "bold" ), command = lambda button = i : click (button ))
145
+ button .grid (row = r , column = c , pady = 1 )
146
+ c += 1
147
+ if c > 7 :
148
+ r += 1
149
+ c = 0
150
+
151
+ # Makes window on loop
152
+ root .mainloop ()
0 commit comments