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