Amazing-Python-Scripts

Форк
0
128 строк · 3.4 Кб
1
# Authour : Yuvraj Kadale - Gssoc 21
2

3
from tkinter import *
4

5
""" 
6
Defining Calculating functions below
7

8
"""
9

10

11
def add(a, b):
12
    return a + b
13

14

15
def sub(a, b):
16
    return a - b
17

18

19
def mul(a, b):
20
    return a * b
21

22

23
def div(a, b):
24
    return a / b
25

26

27
def mod(a, b):
28
    return a % b
29

30

31
def lcm(a, b):
32
    L = a if a > b else b
33
    while L <= a+b:
34
        if L % a == 0 and L % b == 0:
35
            return L
36
        L += 1
37

38

39
def hcf(a, b):
40
    H = a if a < b else b
41
    while H >= 1:
42
        if a % H == 0 and b % H == 0:
43
            return H
44
        H -= 1
45

46

47
def read_from_text(text):
48
    """ Function to extract key words from the sentence """
49
    read = []
50
    for t in text.split(' '):
51
        try:
52
            read.append(float(t))
53
        except ValueError:
54
            pass
55
    return read
56

57

58
def Calculate():
59
    """ Function to calculate the given question """
60

61
    text = text_in.get()  # getting input text
62
    for word in text.split(' '):
63
        if word.upper() in operations.keys():
64
            try:
65
                read = read_from_text(text)  # reading from given text
66
                r = operations[word.upper()](read[0], read[1])
67
                Answer_box.delete(0, END)  # clearing answer box
68
                Answer_box.insert(END, r)
69
            except:
70
                Answer_box.delete(0, END)
71
                # message if there is an input error
72
                Answer_box.insert(
73
                    END, "Somthing went worng, Could you pls retype :')")
74
            finally:
75
                break
76
        elif word.upper() not in operations.keys():
77
            Answer_box.delete(0, END)
78
            Answer_box.insert(
79
                END, "Somthing went worng, Could you pls retype :')")
80

81

82
# Creating Dictionary of possible key words to match with matematical operations
83
operations = {
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
''' 
91
GUI designing begins here with the help of tkinter
92

93
'''
94

95
win = Tk()                                  # Creating Window
96
win.geometry('500x330')                     # Defining windows size
97
win.title("Smart Calcy")                    # title of the window
98
# Defining Background color of the calcy
99
win.configure(bg='Sky blue')
100

101
title_lable = Label(win, text="I am your Smart Calcy :D",
102
                    width=30, padx=3)  # Defining the titile lable
103
title_lable.place(x=160, y=10)
104
intro_lable = Label(win, text="You can call me 'Genius'",
105
                    padx=3)  # Defining the intro lable
106
intro_lable.place(x=200, y=40)
107
# Defining the asking lable
108
ask_lable = Label(
109
    win, text="How may I help you? You can type in below I will read it :)", padx=3)
110
ask_lable.place(x=110, y=130)
111

112
text_in = StringVar()
113
# Entry to take questions from users
114
Question_entry = Entry(win, width=30, textvariable=text_in)
115
Question_entry.place(x=180, y=160)
116

117
# Defining button to ask answer
118
Answer_button = Button(win, text="What do you say?", command=Calculate)
119
Answer_button.place(x=220, y=190)
120

121
Answer_lable = Label(win, text="Genius says ...", padx=3)
122
Answer_lable.place(x=150, y=240)
123

124
# Defining area to answer the given question
125
Answer_box = Listbox(win, width=40, height=3)
126
Answer_box.place(x=150, y=260)
127

128
win.mainloop()
129

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.