|
| 1 | +from tkinter import * |
| 2 | +#function for addition |
| 3 | +def add(a,b): |
| 4 | + return a + b |
| 5 | +#function for subtraction |
| 6 | +def sub(a,b): |
| 7 | + return a - b |
| 8 | +#function for multiplication |
| 9 | +def mul(a,b): |
| 10 | + return a * b |
| 11 | +#function for division |
| 12 | +def div(a,b): |
| 13 | + return a / b |
| 14 | +#function for modulo |
| 15 | +def mod(a,b): |
| 16 | + return a % b |
| 17 | +#function for l.c.m |
| 18 | +def lcm(a,b): |
| 19 | + L = a if a>b else b |
| 20 | + while L <= a*b: |
| 21 | + if L%a == 0 and L%b == 0: |
| 22 | + return L |
| 23 | + L+=1 |
| 24 | +#function for h.c.f. |
| 25 | +def hcf(a,b): |
| 26 | + H = a if a<b else b |
| 27 | + while H >= 1: |
| 28 | + if a%H == 0 and b%H == 0: |
| 29 | + return H |
| 30 | + H-=1 |
| 31 | +#function to extract the needed thing only |
| 32 | +def extract_from_text(text): |
| 33 | + l = [] |
| 34 | + for t in text.split(' '): |
| 35 | + try: |
| 36 | + l.append(float(t)) |
| 37 | + except ValueError: |
| 38 | + pass |
| 39 | + return l |
| 40 | + |
| 41 | +def calculate(): |
| 42 | + text = textin.get() |
| 43 | + for word in text.split(' '): |
| 44 | + if word.upper() in operations.keys(): |
| 45 | + try: |
| 46 | + l = extract_from_text(text) |
| 47 | + r = operations[word.upper()](l[0] , l[1]) |
| 48 | + list.delete(0,END) |
| 49 | + list.insert(END,r) |
| 50 | + except: |
| 51 | + list.delete(0,END) |
| 52 | + list.insert(END,'something went wrong please enter again') |
| 53 | + finally: |
| 54 | + break |
| 55 | + elif word.upper() not in operations.keys(): |
| 56 | + list.delete(0,END) |
| 57 | + list.insert(END,'something went wrong please enter again') |
| 58 | +#for getting the proper function through different keywords |
| 59 | +operations = {'ADD':add , 'ADDITION':add , 'SUM':add , 'PLUS':add , |
| 60 | + 'SUB':sub , 'DIFFERENCE':sub , 'MINUS':sub , 'SUBTRACT':sub, |
| 61 | + 'LCM':lcm , 'HCF':hcf , 'PRODUCT':mul , 'MULTIPLICATION':mul, |
| 62 | + 'MULTIPLY':mul , 'DIVISION':div , 'DIV':div ,'DIVIDE':div, 'MOD':mod , |
| 63 | + 'REMANDER':mod , 'MODULUS':mod} |
| 64 | + |
| 65 | +win = Tk() |
| 66 | +win.geometry('500x300') |
| 67 | +win.title('Smart Helper') |
| 68 | +win.configure(bg='green') |
| 69 | + |
| 70 | +l1 = Label(win , text='Hello there ,I am a smart calculator',width=30 , padx=3) |
| 71 | +l1.place(x=140,y=10) |
| 72 | +l2 = Label(win , text='Thank you for using me !!!' , padx=3) |
| 73 | +l2.place(x=180,y=250) |
| 74 | +l3 = Label(win , text='What can i help you' , padx=3) |
| 75 | +l3.place(x=176,y=40) |
| 76 | + |
| 77 | +textin = StringVar() |
| 78 | +e1 = Entry(win , width=30 , textvariable = textin) |
| 79 | +e1.place(x=140,y=70) |
| 80 | + |
| 81 | +b1 = Button(win , text='Find this' ,command=calculate) |
| 82 | +b1.place(x=200,y=100) |
| 83 | + |
| 84 | +list = Listbox(win,width=20,height=3) |
| 85 | +list.place(x=170,y=125) |
| 86 | + |
| 87 | +win.mainloop() |
0 commit comments