Advertisement
IshaanGarud

Calculator#1 using TK

Jan 17th, 2022 (edited)
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.51 KB | None | 0 0
  1. """ A simple Calculator made by Inu
  2. Just a simple project by a Kid
  3. It has in-built A.I. too!
  4. Try it out for yourself by doing 2+2.
  5.  
  6. I dunno about Copyright....
  7. still , Inu©
  8.  
  9. """
  10.  
  11. from tkinter import *
  12. import tkinter.font as font
  13. import math
  14. from time import strftime
  15.  
  16. root = Tk()
  17. root.title("First GUI Calculator project by Inu :)")
  18. root.geometry("690x1485")
  19.  
  20. headfont = font.Font(family="Comic Sans", size=8, weight="bold")
  21. headline = Label(root,
  22.              text = "A simple Calculator [12|1|22]. \n",
  23.              fg = "green",
  24.              font=headfont)
  25. headline.grid(row=0, column=0)
  26.  
  27. # ----------------------------------------------------------------                      
  28.  
  29. label1 = Label(root, text="First Number:- ")                                                  
  30. inp1 = Entry(root)
  31. label2 = Label(root, text="Second Number:- ")                                                  
  32. inp2 = Entry(root)
  33.  
  34. answer = Label()
  35. wisdom = Label()
  36. an_mode = "Deg"
  37. f_mode = "normal"
  38.  
  39. #---------------Clock/Watch-----------------------------
  40. clock = Label(fg="green", font=headfont, bg="#BFFF00")  
  41. def time():
  42.    
  43.     string = strftime('%H:%M:%S %p')      
  44.     clock.config(text = string)                            
  45.     clock.after(1000, time)
  46.  
  47. clock.grid(row=0, column=1)
  48. time()
  49. gaali = "Chutiya hai kya? \n itna choota bhi nahi aata?"
  50. #--------*Extra Functions*------------------------------------
  51. def NullSafe(n1, n2):
  52.     try:
  53.         num1 = float(inp1.get())
  54.     except:
  55.         num1 = n1      
  56.     try:
  57.         num2 = float(inp2.get())
  58.     except:      
  59.         num2 = n2
  60.     return num1, num2
  61.  
  62. def intelligence(a, b, res):
  63.     if a<10 or b<10:
  64.         answer["text"], answer["fg"], answer["font"] = gaali, "red", headfont
  65.     else:
  66.         answer["fg"] = "black"
  67.         answer["font"] ="TkDefaultFont"
  68.  
  69. def Noice(res):
  70.     if res==69 or res==420 or res==80085:
  71.         answer["text"] = "%d Nice ಠ◡ಠ" % res
  72.  
  73. #-----------Main_Logic---------------------------------                
  74. def Add():
  75.     a, b = NullSafe(0, 0)
  76.     answer["text"] = result = a+b
  77.     Noice(result)
  78.     intelligence(a, b, result)  
  79. def Sub():
  80.     a, b = NullSafe(0, 0)
  81.     answer["text"] = result = a-b
  82.     Noice(result)
  83.     intelligence(a, b, result)  
  84. def Mul():
  85.     a, b = NullSafe(1, 1)
  86.     answer["text"] = result = a*b
  87.     Noice(result)
  88.     intelligence (a, b, result)    
  89. def Div():
  90.     a, b = NullSafe(0, 1)
  91.     answer["text"] = result = a/b
  92.     Noice(result)
  93.     intelligence (a, b, result)
  94. def Root():
  95.     a, b = NullSafe(0, 2)
  96.     answer["text"] = result = a**(1/b)
  97.     answer["fg"], answer["font"] = "black", "TkDefaultFont"
  98.     Noice(result)
  99. def Pow():
  100.     a, b = NullSafe(0, 1)
  101.     answer["text"] = result = a**b
  102.     answer["fg"], answer["font"] = "black", "TkDefaultFont"
  103.     Noice(result)
  104.     #intelligence (20, 20, result)
  105. def Abs():
  106.     a, b = NullSafe(0, 0)
  107.     #b = int(inp2.get())
  108.     answer["text"] = result = abs(a)
  109.     Noice(result)
  110.     #intelligence (20, 20, result)
  111. def GeoMean():
  112.     a, b = NullSafe(1, 1)
  113.     mean = (a*b)
  114.     answer["text"] = result = mean**0.5
  115.     Noice(result)
  116.     #intelligence (20, 20, result)
  117. def SumUp():
  118.     a, b = NullSafe(0, 0)
  119.     answer["text"] = result = ((a*(a+1))//2) - ((b*(b+1))//2)
  120.     Noice(result)
  121.     #intelligence (20, 20, result)
  122. def Factorial():
  123.     a, b = NullSafe(0, 0)
  124.     answer["text"] = result = (math.factorial(a))
  125.     Noice(result)
  126. def Log():
  127.     a, b = NullSafe(0, 0)
  128.     answer["text"] = result = math.log(a, b)
  129.     Noice(result)
  130.    
  131. def Deg_Rad():
  132.     global an_mode
  133.     an_mode = "Deg"
  134.     if b_deg_rad["text"] == "Deg":
  135.         b_deg_rad["text"] = "Rad"
  136.         an_mode = "Rad"
  137.        
  138.     elif b_deg_rad["text"] == "Rad":
  139.         b_deg_rad["text"] = "Deg"
  140.         an_mode = "Deg"
  141.  
  142. def Change():
  143.      global f_mode
  144.      if f_mode == "normal":
  145.         f_mode = "inverse"
  146.         b_sine["text"] = "arcsin(a)"
  147.         b_cosine["text"] = "arccos(a)"
  148.         b_tangent["text"] = "arctan(a)"
  149.        
  150.      elif f_mode == "inverse":
  151.         f_mode = "normal"  
  152.         b_sine["text"] = "sin(a)"
  153.         b_cosine["text"] = "cos(a)"
  154.         b_tangent["text"] = "tan(a)"
  155.        
  156. def Sine():
  157.     a = int(inp1.get())
  158.     if f_mode == "normal":  
  159.         if an_mode == "Deg":
  160.             answer["text"] = math.sin(math.radians(a))
  161.         elif an_mode == "Rad":
  162.             answer["text"] = math.sin(a)
  163.                      
  164.     if f_mode == "inverse":
  165.             answer["text"] = math.asin(math.radians(a))  
  166.              
  167. def Cosine():
  168.     a = int(inp1.get())
  169.     if f_mode == "normal":  
  170.         if an_mode == "Deg":
  171.             answer["text"] = math.cos(math.radians(a))
  172.         elif an_mode == "Rad":
  173.             answer["text"] = math.cos(a)
  174.                      
  175.     if f_mode == "inverse":
  176.             answer["text"] = math.acos(math.radians(a))  
  177.    
  178. def Tangent():
  179.     a = int(inp1.get())
  180.     if f_mode == "normal":  
  181.         if an_mode == "Deg":
  182.             answer["text"] = math.tan(math.radians(a))
  183.         elif an_mode == "Rad":
  184.             answer["text"] = math.tan(a)
  185.                      
  186.     if f_mode == "inverse":
  187.             answer["text"] = math.atan(math.radians(a))
  188.            
  189. def Clear():
  190.     inp1.delete(0, END)  
  191.     inp2.delete(0, END)
  192.     answer["text"] = ""
  193.              
  194. #-------------*Buttons*---------------------------------
  195. b_clear = Button(root, text="C", command=Clear, bg="white", fg="red")
  196. b_add = Button(root, text="+", command=Add, bg="grey")
  197. b_sub = Button(root, text="-", command=Sub, bg="grey")
  198. b_mul = Button(root, text="×", command=Mul, bg="grey")
  199. b_div = Button(root, text="÷", command=Div, bg="grey")
  200. b_root = Button(root, text="√", command=Root, bg="grey")
  201. b_power = Button(root, text="^", command=Pow, bg="grey")
  202. b_abs = Button(root, text="absolute(No.1)", command=Abs, bg="orange")
  203. b_geomean = Button(root, text="geo_mean", command=GeoMean, bg="orange")
  204. b_sumUp = Button(root, text="Σ", command=SumUp, bg="lime")
  205. b_factorial = Button(root, text="(Number1)!", command=Factorial, bg="lime")
  206. b_log = Button(root, text="log_b a", command=Log, bg = "lime")
  207. b_deg_rad = Button(root, text="Deg", command=Deg_Rad)
  208. b_f_mode = Button(root, text="\u21c6", command=Change)
  209. b_sine = Button(root, text="sin(a)", command=Sine, bg="#CBC3E3")
  210. b_cosine = Button(root, text="cosine(a)", command=Cosine, bg="#CBC3E3")
  211. b_tangent = Button(root, text="tangent(a)", command=Tangent, bg="#CBC3E3")
  212.  
  213. #---------*Setting_Buttons & Text*-----------------------------------------
  214. label1.grid(row=1, column=0)
  215. inp1.grid(row=1, column=1)
  216. label2.grid(row=2, column=0)
  217. inp2.grid(row=2, column=1)
  218.  
  219. b_add.grid(row=4, column=0)
  220. b_sub.grid(row=4, column=1)
  221. b_mul.grid(row=5, column=0)
  222. b_div.grid(row=5, column=1)
  223. b_root.grid(row=6, column=0)
  224. b_power.grid(row=6, column=1)
  225. b_abs.grid(row=7, column=0)
  226. b_geomean.grid(row=7, column=1)
  227. b_sumUp.grid(row=8, column=0)
  228. b_factorial.grid(row=8, column=1)
  229. b_log.grid(row=9, column=0)
  230.  
  231. b_f_mode.grid(row=10, column=1)
  232. b_deg_rad.grid(row=10, column=0)
  233. b_sine.grid(row=11, column=0)
  234. b_cosine.grid(row=11, column=1)
  235. b_tangent.grid(row=12, column=0)
  236.  
  237. b_clear.grid(row=3, column=0)
  238. answer.grid(row=100, column=0)
  239.  
  240.  
  241. # ----------------------------------------------------------------                      
  242.  
  243. root.mainloop()
  244.  
  245.  
  246. #++++++++++++++++
  247. """ Code for Null Safety:
  248.    try:
  249.        a = int(inp1.get())
  250.    except:
  251.        a = 0  
  252.    try:
  253.        b = int(inp2.get())  
  254.    except:
  255.        b = 0
  256. """
  257.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement