|
| 1 | +################################################### Scientific Calculator ################################################### |
| 2 | +################################################## Made By Avdhesh Varshney ################################################# |
| 3 | +############################################ (https://github.com/Avdhesh-Varshney) ########################################## |
| 4 | + |
| 5 | +# Importing Modules |
| 6 | +from tkinter import * |
| 7 | +import tkinter.messagebox as tmsg |
| 8 | +import math as m |
| 9 | + |
| 10 | +# Initialising Tkinter module |
| 11 | +root = Tk() |
| 12 | + |
| 13 | +# Fixing the size of the calculator screen |
| 14 | +root.minsize(520,340) |
| 15 | +root.maxsize(520,340) |
| 16 | + |
| 17 | +# Title of the calculator |
| 18 | +root.title("Scientific Calculator") |
| 19 | +root.wm_iconbitmap("calculator.ico") |
| 20 | + |
| 21 | +sc = StringVar() |
| 22 | +sc = Entry(root,width=31,textvariable=sc,relief=SUNKEN,font="cosmicsansms 20") |
| 23 | +sc.grid(row=0,column=0,columnspan=10,padx=11,pady=12) |
| 24 | + |
| 25 | +# Helping messages |
| 26 | +def helper(): |
| 27 | + help = '''1. For the following functions please enter the number first and then press the required function: |
| 28 | +sin, cos, tan, log, ln, √, !, rad, degree, 1/x, π, e |
| 29 | +
|
| 30 | +2. For multiplication with float numbers, say 5*0.4 multiply like 5*4/10''' |
| 31 | + # Adding message into the menu bar of the calculator |
| 32 | + tmsg.showinfo("Help",help) |
| 33 | + |
| 34 | + |
| 35 | +# About section of the header |
| 36 | +def abt(): |
| 37 | + abt_text = "This calculator was developed using Tkinter, by Avdhesh Varshney.\n\n" |
| 38 | + abt_text += "A student of Dr. B R Ambedkar National Institute of Technology, Jalandhar.\n\n" |
| 39 | + abt_text += "Click the link below to access the GitHub account:\n\n" |
| 40 | + abt_text += "https://github.com/Avdhesh-Varshney" |
| 41 | + |
| 42 | + # Appending the link to the message in the about section of the menu bar |
| 43 | + tmsg.showinfo("About", abt_text) |
| 44 | + |
| 45 | + |
| 46 | +# Suggestion to the user of the scientific calculator |
| 47 | +def const(): |
| 48 | + msg = '''If you press constants like: π and e, 2 times, the result will be square of that constant. |
| 49 | +That means number of times you press the constant, the result will be constant to the power that number. ''' |
| 50 | + tmsg.showinfo("Help",msg) |
| 51 | + |
| 52 | + |
| 53 | +# Initialising main screen/root of the calculator |
| 54 | +mainmenu = Menu(root) |
| 55 | + |
| 56 | +# Adding menu bar in the calculator |
| 57 | +submenu = Menu(mainmenu,tearoff=0) |
| 58 | +submenu.add_command(label="General",command=helper) |
| 59 | +submenu.add_command(label="Constants",command=const) |
| 60 | +mainmenu.add_cascade(label="Help",menu=submenu) |
| 61 | + |
| 62 | +# Append the author details |
| 63 | +mainmenu.add_command(label="About",command=abt) |
| 64 | + |
| 65 | +# Append the exit button |
| 66 | +mainmenu.add_command(label="Exit",command=quit) |
| 67 | + |
| 68 | +# Finally set the configuration in the screen of the calculator made by Tkinter library |
| 69 | +root.config(menu=mainmenu) |
| 70 | + |
| 71 | + |
| 72 | +# Creating a function which helps to calculate the different function in scientific calculator |
| 73 | +def scientificCalc(event): |
| 74 | + # After event is triggered, performing different operation according to the command of the user |
| 75 | + key = event.widget |
| 76 | + text = key['text'] |
| 77 | + val = sc.get() |
| 78 | + sc.delete(0,END) |
| 79 | + |
| 80 | + # Checking the function i.e., which function is triggered by the user |
| 81 | + |
| 82 | + # Trigonometric functions |
| 83 | + if text == "sin": |
| 84 | + sc.insert(0, m.sin(float(val))) |
| 85 | + |
| 86 | + elif text == "cos": |
| 87 | + sc.insert(0, m.cos(float(val))) |
| 88 | + |
| 89 | + elif text == "tan": |
| 90 | + sc.insert(0, m.tan(float(val))) |
| 91 | + |
| 92 | + # Logarithmic functions |
| 93 | + elif text == "log": |
| 94 | + if(float(val) <= 0.00): |
| 95 | + sc.insert(0, "Not Possible") |
| 96 | + else: |
| 97 | + sc.insert(0, m.log10(float(val))) |
| 98 | + |
| 99 | + elif text == "ln": |
| 100 | + if(float(val) <= 0.00): |
| 101 | + sc.insert(0, "Not Possible") |
| 102 | + else: |
| 103 | + sc.insert(0, m.log(float(val))) |
| 104 | + |
| 105 | + # Exponential functions |
| 106 | + elif text == "√": |
| 107 | + sc.insert(0, m.sqrt(float(val))) |
| 108 | + |
| 109 | + elif text == "!": |
| 110 | + sc.insert(0, m.factorial(int(val))) |
| 111 | + |
| 112 | + # Angular functions |
| 113 | + elif text == "rad": |
| 114 | + sc.insert(0, m.radians(float(val))) |
| 115 | + |
| 116 | + elif text == "deg": |
| 117 | + sc.insert(0, m.degrees(float(val))) |
| 118 | + |
| 119 | + # Constants functions |
| 120 | + elif text == "π": |
| 121 | + if val == "": |
| 122 | + ans = str(m.pi) |
| 123 | + sc.insert(0, ans) |
| 124 | + else: |
| 125 | + ans = str(float(val) * (m.pi)) |
| 126 | + sc.insert(0, ans) |
| 127 | + |
| 128 | + elif text == "1/x": |
| 129 | + if(val == "0"): |
| 130 | + sc.insert(0, "ꝏ") |
| 131 | + else: |
| 132 | + sc.insert(0, 1/float(val)) |
| 133 | + |
| 134 | + elif text == "e": |
| 135 | + if val == "": |
| 136 | + sc.insert(0, str(m.e)) |
| 137 | + else: |
| 138 | + sc.insert(0, str(float(val) * (m.e))) |
| 139 | + |
| 140 | + |
| 141 | +# Function to check click events |
| 142 | +def click(event): |
| 143 | + key = event.widget |
| 144 | + text = key['text'] |
| 145 | + oldValue = sc.get() |
| 146 | + sc.delete(0,END) |
| 147 | + newValue = oldValue + text |
| 148 | + sc.insert(0,newValue) |
| 149 | + |
| 150 | + |
| 151 | +# Clear the calculator screen |
| 152 | +def clr(event): |
| 153 | + sc.delete(0,END) |
| 154 | + |
| 155 | + |
| 156 | +# Delete the entered text or number one by one |
| 157 | +def backspace(event): |
| 158 | + entered = sc.get() |
| 159 | + length = len(entered) - 1 |
| 160 | + sc.delete(length, END) |
| 161 | + |
| 162 | + |
| 163 | +# Calculate function is triggered |
| 164 | +def calculate(event): |
| 165 | + answer = sc.get() |
| 166 | + if "^" in answer: |
| 167 | + answer = answer.replace("^", "**") |
| 168 | + answer = eval(answer) |
| 169 | + sc.delete(0, END) |
| 170 | + sc.insert(0, answer) |
| 171 | + |
| 172 | + |
| 173 | +# Initialising the user interface of the calculator |
| 174 | +class Calculator: |
| 175 | + def __init__(self, txt, r, c, funcName, color="black"): |
| 176 | + self.var = Button(root, text=txt, padx=3, pady=5, fg="white", bg=color, width=10, font="cosmicsansms 12") |
| 177 | + self.var.bind("<Button-1>", funcName) |
| 178 | + self.var.grid(row=r, column=c) |
| 179 | + |
| 180 | + |
| 181 | +# Adding labels on the different buttons with name, color, function, and indexes |
| 182 | + |
| 183 | +# Trignometric fuctions |
| 184 | + |
| 185 | +btn0 = Calculator("sin", 1, 0, scientificCalc, "grey") |
| 186 | + |
| 187 | +btn1 = Calculator("cos", 1, 1, scientificCalc, "grey") |
| 188 | + |
| 189 | +btn2 = Calculator("tan", 1, 2, scientificCalc, "grey") |
| 190 | + |
| 191 | + |
| 192 | +# Logarithmic functions |
| 193 | + |
| 194 | +btn3 = Calculator("log", 1, 3, scientificCalc) |
| 195 | + |
| 196 | +btn4 = Calculator("ln", 1, 4, scientificCalc) |
| 197 | + |
| 198 | + |
| 199 | +# Brackets |
| 200 | + |
| 201 | +btn5 = Calculator("(", 2, 0, click) |
| 202 | + |
| 203 | +btn6 = Calculator(")", 2, 1, click) |
| 204 | + |
| 205 | + |
| 206 | +# Exponential functions |
| 207 | + |
| 208 | +btn7 = Calculator("^", 2, 2, click) |
| 209 | + |
| 210 | +btn8 = Calculator("√", 2, 3, scientificCalc) |
| 211 | + |
| 212 | +btn9 = Calculator("!", 2, 4, scientificCalc) |
| 213 | + |
| 214 | + |
| 215 | +# Constant and Angular functions |
| 216 | + |
| 217 | +btn10 = Calculator("π", 3, 0, scientificCalc, "blue") |
| 218 | + |
| 219 | +btn11 = Calculator("1/x", 3, 1, scientificCalc) |
| 220 | + |
| 221 | +btn12 = Calculator("deg", 3, 2, scientificCalc) |
| 222 | + |
| 223 | +btn13 = Calculator("rad", 3, 3, scientificCalc) |
| 224 | + |
| 225 | +btn14 = Calculator("e", 3, 4, scientificCalc, "blue") |
| 226 | + |
| 227 | + |
| 228 | +# Basic Arithmetic operators |
| 229 | + |
| 230 | +btn15 = Calculator("/", 4, 0, click, "#DBA800") |
| 231 | + |
| 232 | +btn16 = Calculator("*", 4, 1, click, "#DBA800") |
| 233 | + |
| 234 | +btn17 = Calculator("-", 4, 2, click, "#DBA800") |
| 235 | + |
| 236 | +btn18 = Calculator("+", 4, 3, click, "#DBA800") |
| 237 | + |
| 238 | +btn19 = Calculator("%", 4, 4, click, "#DBA800") |
| 239 | + |
| 240 | + |
| 241 | +# Numbering Buttons |
| 242 | + |
| 243 | +btn20 = Calculator("9", 5, 0, click) |
| 244 | + |
| 245 | +btn21 = Calculator("8", 5, 1, click) |
| 246 | + |
| 247 | +btn22 = Calculator("7", 5, 2, click) |
| 248 | + |
| 249 | +btn23 = Calculator("6", 5, 3, click) |
| 250 | + |
| 251 | +btn24 = Calculator("5", 5, 4, click) |
| 252 | + |
| 253 | +btn25 = Calculator("4", 6, 0, click) |
| 254 | + |
| 255 | +btn26 = Calculator("3", 6, 1, click) |
| 256 | + |
| 257 | +btn27 = Calculator("2", 6, 2, click) |
| 258 | + |
| 259 | +btn28 = Calculator("1", 6, 3, click) |
| 260 | + |
| 261 | +btn29 = Calculator("0", 6, 4, click) |
| 262 | + |
| 263 | + |
| 264 | +# Some special buttons |
| 265 | + |
| 266 | +btn30 = Calculator("C", 7, 0, clr, "red") |
| 267 | + |
| 268 | +btn31 = Calculator("⌦", 7, 1, backspace, "red") |
| 269 | + |
| 270 | +btn32 = Calculator("00", 7, 2, click) |
| 271 | + |
| 272 | +btn33 = Calculator(".", 7, 3, click) |
| 273 | + |
| 274 | +btn34 = Calculator("=", 7, 4, calculate) |
| 275 | + |
| 276 | +# Program Starts |
| 277 | +root.mainloop() |
| 278 | + |
0 commit comments