# Calculator#1 using TK

Jan 17th, 2022 (edited)
679
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. """ A simple Calculator made by Inu
2. Just a simple project by a Kid who broke his eye a day ago
3. It has in-built A.I. too!
4. Try it out for yourself by doing 2+2.
5.
8.
9. """
10.
11.
12. from tkinter import *
13. import tkinter.font as font
14. import math
15. import time
16. from time import strftime
17.
18.
19. root = Tk()
20. root.title("First GUI Calculator project by Inu :)")
21. root.geometry("690x1485")
22.
23.
24. headfont = font.Font(family="Comic Sans", size=8, weight="bold")
26.              text = "A simple Calculator [12|1|22]. \n",
27.              fg = "green",
30.
31. # ----------------------------------------------------------------
32.
33. label1 = Label(root, text="First Number:- ")
34. inp1 = Entry(root)
35. label2 = Label(root, text="Second Number:- ")
36. inp2 = Entry(root)
37.
39. wisdom = Label()
40.
41.
42. #---------------Clock/Watch-----------------------------
43. clock = Label(fg="green", font=headfont, bg="#BFFF00")
44. def time():
45.     string = strftime('%H:%M:%S %p')
46.     clock.config(text = string)
47.     clock.after(1000, time)
48.
49. clock.grid(row=0, column=1)
50. time()
51.
52.
53. #--------*Extra Functions*------------------------------------
54. def NullSafe(n1, n2):
55.     try:
56.         num1 = int(inp1.get())
57.     except:
58.         num1 = n1
59.     try:
60.         num2 = int(inp2.get())
61.     except:
62.         num2 = n2
63.     return num1, num2
64.
65. def intelligence(a, b, res):
66.     if a<10 or b<10:
68.     else:
71.
72. def Noice(res):
73.     if res==69 or res==420 or res==80085:
74.         answer["text"] = res, "Nice ಠ◡ಠ"
75.
76.
77. #-----------Main_Logic---------------------------------
79.     a, b = NullSafe(0, 0)
80.     answer["text"] = result = a+b
81.     Noice(result)
82.     #intelligence(a, b, result)
83. def Sub():
84.     a, b = NullSafe(0, 0)
85.     answer["text"] = result = a-b
86.     Noice(result)
87.     #intelligence(a, b, result)
88. def Mul():
89.     a, b = NullSafe(1, 1)
90.     answer["text"] = result = a*b
91.     Noice(result)
92.     #intelligence (20, 20, result)
93. def Div():
94.     a, b = NullSafe(0, 1)
95.     answer["text"] = result = a/b
96.     Noice(result)
97.     #intelligence (20, 20, result)
98. def Root():
99.     a, b = NullSafe(0, 2)
100.     answer["text"] = result = a**(1/b)
102.     Noice(result)
103. def Pow():
104.     a, b = NullSafe(0, 1)
105.     answer["text"] = result = a**b
107.     Noice(result)
108.     #intelligence (20, 20, result)
109. def Abs():
110.     a, b = NullSafe(0, 0)
111.     #b = int(inp2.get())
112.     answer["text"] = result = abs(a)
113.     Noice(result)
114.     #intelligence (20, 20, result)
115. def GeoMean():
116.     a, b = NullSafe(1, 1)
117.     mean = (a*b)
118.     answer["text"] = result = mean**0.5
119.     Noice(result)
120.     #intelligence (20, 20, result)
121. def SumUp():
122.     a, b = NullSafe(0, 0)
123.     answer["text"] = result = ((a*(a+1))//2) - ((b*(b+1))//2)
124.     Noice(result)
125.     #intelligence (20, 20, result)
126. def Factorial():
127.     a, b = NullSafe(0, 0)
128.     answer["text"] = result = (math.factorial(a))
129.     Noice(result)
130.
131.
132. #-------------*Buttons*---------------------------------
134. b_sub = Button(root, text="-", command=Sub, bg="grey")
135. b_mul = Button(root, text="×", command=Mul, bg="grey")
136. b_div = Button(root, text="÷", command=Div, bg="grey")
137. b_root = Button(root, text="√", command=Root, bg="grey")
138. b_power = Button(root, text="^", command=Pow, bg="grey")
139. b_abs = Button(root, text="absolute(No.1)", command=Abs, bg="orange")
140. b_geomean = Button(root, text="geo_mean", command=GeoMean, bg="orange")
141. b_sumUp = Button(root, text="Σ", command=SumUp, bg="lime")
142. b_factorial = Button(root, text="(Number1)!", command=Factorial, bg="lime")
143.
144.
145.
146. #---------*Setting_Buttons & Text*-----------------------------------------
147. label1.grid(row=1, column=0)
148. inp1.grid(row=1, column=1)
149. label2.grid(row=2, column=0)
150. inp2.grid(row=2, column=1)
151.
153. b_sub.grid(row=3, column=1)
154. b_mul.grid(row=4, column=0)
155. b_div.grid(row=4, column=1)
156. b_root.grid(row=5, column=0)
157. b_power.grid(row=5, column=1)
158. b_abs.grid(row=6, column=0)
159. b_geomean.grid(row=6, column=1)
160. b_sumUp.grid(row=7, column=0)
161. b_factorial.grid(row=7, column=1)
162.
164.
165.
166. # ----------------------------------------------------------------
167.
168. root.mainloop()
169.
170.
171. """def Sine():
172.    a = int(inp1.get())
173.    temp = math.sin(a)
175. def Cosine():
176.    a = int(inp1.get())
178. def Tangent():
179.    a = int(inp1.get())
181.
182.
183. #b_sine = Button(root, text="sin(a)", command=Sine, bg="#CBC3E3")
184. #b_cosine = Button(root, text="cosine(a)", command=Cosine, bg="#CBC3E3")
185. #b_tangent = Button(root, text="tangent(a)", command=Tangent, bg="#CBC3E3")
186.
187.
188. #b_sine.grid(row=8, column=0)
189. #b_cosine.grid(row=8, column=1)
190. #b_tangent.grid(row=9, column=0)
191.
192.
193. #++++++++++++++++
194. """ Code for Null Safety:
195.    try:
196.        a = int(inp1.get())
197.    except:
198.        a = 0
199.    try:
200.        b = int(inp2.get())
201.    except:
202.        b = 0
203. """
204.
RAW Paste Data Copied