Amazing-Python-Scripts
128 строк · 3.4 Кб
1# Authour : Yuvraj Kadale - Gssoc 21
2
3from tkinter import *4
5"""
6Defining Calculating functions below
7
8"""
9
10
11def add(a, b):12return a + b13
14
15def sub(a, b):16return a - b17
18
19def mul(a, b):20return a * b21
22
23def div(a, b):24return a / b25
26
27def mod(a, b):28return a % b29
30
31def lcm(a, b):32L = a if a > b else b33while L <= a+b:34if L % a == 0 and L % b == 0:35return L36L += 137
38
39def hcf(a, b):40H = a if a < b else b41while H >= 1:42if a % H == 0 and b % H == 0:43return H44H -= 145
46
47def read_from_text(text):48""" Function to extract key words from the sentence """49read = []50for t in text.split(' '):51try:52read.append(float(t))53except ValueError:54pass55return read56
57
58def Calculate():59""" Function to calculate the given question """60
61text = text_in.get() # getting input text62for word in text.split(' '):63if word.upper() in operations.keys():64try:65read = read_from_text(text) # reading from given text66r = operations[word.upper()](read[0], read[1])67Answer_box.delete(0, END) # clearing answer box68Answer_box.insert(END, r)69except:70Answer_box.delete(0, END)71# message if there is an input error72Answer_box.insert(73END, "Somthing went worng, Could you pls retype :')")74finally:75break76elif word.upper() not in operations.keys():77Answer_box.delete(0, END)78Answer_box.insert(79END, "Somthing went worng, Could you pls retype :')")80
81
82# Creating Dictionary of possible key words to match with matematical operations
83operations = {84'ADD': add, 'ADDITION': add, 'SUM': add, 'PLUS': add,85'SUB': sub, 'MINUS': sub, 'DIFFERENCE': sub, 'SUBTRACT': sub,86'LCM': lcm, 'HCF': hcf, 'PRODUCT': mul, 'MULTIPLICATION': mul, 'MULTIPLY': mul,87'DIVIDE': div, 'DIVISION': div, 'DIV': div, 'MOD': mod, 'REMINDER': mod, 'MODULUS': mod,88}
89
90'''
91GUI designing begins here with the help of tkinter
92
93'''
94
95win = Tk() # Creating Window96win.geometry('500x330') # Defining windows size97win.title("Smart Calcy") # title of the window98# Defining Background color of the calcy
99win.configure(bg='Sky blue')100
101title_lable = Label(win, text="I am your Smart Calcy :D",102width=30, padx=3) # Defining the titile lable103title_lable.place(x=160, y=10)104intro_lable = Label(win, text="You can call me 'Genius'",105padx=3) # Defining the intro lable106intro_lable.place(x=200, y=40)107# Defining the asking lable
108ask_lable = Label(109win, text="How may I help you? You can type in below I will read it :)", padx=3)110ask_lable.place(x=110, y=130)111
112text_in = StringVar()113# Entry to take questions from users
114Question_entry = Entry(win, width=30, textvariable=text_in)115Question_entry.place(x=180, y=160)116
117# Defining button to ask answer
118Answer_button = Button(win, text="What do you say?", command=Calculate)119Answer_button.place(x=220, y=190)120
121Answer_lable = Label(win, text="Genius says ...", padx=3)122Answer_lable.place(x=150, y=240)123
124# Defining area to answer the given question
125Answer_box = Listbox(win, width=40, height=3)126Answer_box.place(x=150, y=260)127
128win.mainloop()129