Amazing-Python-Scripts

Форк
0
190 строк · 5.5 Кб
1
import tkinter as tk
2

3
root = tk.Tk()  # Main box window
4
root.title("Standard Calculator")  # Title shown at the title bar
5
root.resizable(0, 0)  # disabling the resizeing of the window
6

7
# Creating an entry field:
8
e = tk.Entry(root,
9
             width=35,
10
             bg='#f0ffff',
11
             fg='black',
12
             borderwidth=5,
13
             justify='right',
14
             font='Calibri 15')
15
e.grid(row=0, column=0, columnspan=3, padx=12, pady=12)
16

17

18
def buttonClick(num):  # function for clicking
19
    temp = e.get(
20
    )  # temporary varibale to store the current input in the screen
21
    e.delete(0, tk.END)  # clearing the screen from index 0 to END
22
    e.insert(0, temp + num)  # inserting the incoming number input
23

24

25
def buttonClear():  # function for clearing
26
    e.delete(0, tk.END)
27

28

29
def buttonGet(
30
        oper
31
):  # function for storing the first input and printing '+, -, /, *'
32
    global num1, math  # global variable num1 and math to use in function buttonEqual()
33
    num1 = e.get()  # getting first number
34
    math = oper  # oper varaible is the type of operation being performed
35
    e.insert(tk.END, math)
36
    try:
37
        num1 = float(num1)  # converting the number to float type
38
    except ValueError:  # in case there is a character other than numerals, clear the screen
39
        buttonClear()
40

41

42
def buttonEqual():  # function for printing the sum
43
    inp = e.get()  # getting the inserted input
44
    num2 = float(inp[inp.index(math) + 1:])  # getting the second number
45
    e.delete(0, tk.END)
46
    if math == '+':  # Addition
47
        e.insert(0, str(num1 + num2))
48
    elif math == '-':  # Subtraction
49
        e.insert(0, str(num1 - num2))
50
    elif math == 'x':  # Multiplication
51
        e.insert(0, str(num1 * num2))
52
    elif math == '/':  # Division
53
        try:
54
            e.insert(0, str(num1 / num2))
55
        except ZeroDivisionError:
56
            # in case there is a zero in the denominator, answer is undefined
57
            e.insert(0, 'Undefined')
58

59

60
# Defining Buttons:
61
b1 = tk.Button(root,
62
               text='1',
63
               padx=40,
64
               pady=10,
65
               command=lambda: buttonClick('1'),
66
               font='Calibri 12')
67
b2 = tk.Button(root,
68
               text='2',
69
               padx=40,
70
               pady=10,
71
               command=lambda: buttonClick('2'),
72
               font='Calibri 12')
73
b3 = tk.Button(root,
74
               text='3',
75
               padx=40,
76
               pady=10,
77
               command=lambda: buttonClick('3'),
78
               font='Calibri 12')
79
b4 = tk.Button(root,
80
               text='4',
81
               padx=40,
82
               pady=10,
83
               command=lambda: buttonClick('4'),
84
               font='Calibri 12')
85
b5 = tk.Button(root,
86
               text='5',
87
               padx=40,
88
               pady=10,
89
               command=lambda: buttonClick('5'),
90
               font='Calibri 12')
91
b6 = tk.Button(root,
92
               text='6',
93
               padx=40,
94
               pady=10,
95
               command=lambda: buttonClick('6'),
96
               font='Calibri 12')
97
b7 = tk.Button(root,
98
               text='7',
99
               padx=40,
100
               pady=10,
101
               command=lambda: buttonClick('7'),
102
               font='Calibri 12')
103
b8 = tk.Button(root,
104
               text='8',
105
               padx=40,
106
               pady=10,
107
               command=lambda: buttonClick('8'),
108
               font='Calibri 12')
109
b9 = tk.Button(root,
110
               text='9',
111
               padx=40,
112
               pady=10,
113
               command=lambda: buttonClick('9'),
114
               font='Calibri 12')
115
b0 = tk.Button(root,
116
               text='0',
117
               padx=40,
118
               pady=10,
119
               command=lambda: buttonClick('0'),
120
               font='Calibri 12')
121
bdot = tk.Button(root,
122
                 text='.',
123
                 padx=41,
124
                 pady=10,
125
                 command=lambda: buttonClick('.'),
126
                 font='Calibri 12')
127

128
badd = tk.Button(root,
129
                 text='+',
130
                 padx=29,
131
                 pady=10,
132
                 command=lambda: buttonGet('+'),
133
                 font='Calibri 12')
134
bsub = tk.Button(root,
135
                 text='-',
136
                 padx=30,
137
                 pady=10,
138
                 command=lambda: buttonGet('-'),
139
                 font='Calibri 12')
140
bmul = tk.Button(root,
141
                 text='x',
142
                 padx=30,
143
                 pady=10,
144
                 command=lambda: buttonGet('x'),
145
                 font='Calibri 12')
146
bdiv = tk.Button(root,
147
                 text='/',
148
                 padx=30.5,
149
                 pady=10,
150
                 command=lambda: buttonGet('/'),
151
                 font='Calibri 12')
152

153
bclear = tk.Button(root,
154
                   text='AC',
155
                   padx=20,
156
                   pady=10,
157
                   command=buttonClear,
158
                   font='Calibri 12')
159
bequal = tk.Button(root,
160
                   text='=',
161
                   padx=39,
162
                   pady=10,
163
                   command=buttonEqual,
164
                   font='Calibri 12')
165

166
# Putting the buttons on the screen:
167
b1.grid(row=3, column=0)
168
b2.grid(row=3, column=1)
169
b3.grid(row=3, column=2)
170
badd.grid(row=3, column=3)
171

172
b4.grid(row=2, column=0)
173
b5.grid(row=2, column=1)
174
b6.grid(row=2, column=2)
175
bmul.grid(row=2, column=3)
176

177
b7.grid(row=1, column=0)
178
b8.grid(row=1, column=1)
179
b9.grid(row=1, column=2)
180
bdiv.grid(row=1, column=3)
181

182
b0.grid(row=4, column=0)
183
bdot.grid(row=4, column=1)
184
bequal.grid(row=4, column=2)
185
bsub.grid(row=4, column=3)
186

187
bclear.grid(row=0, column=3)
188

189
# Looping the window:
190
root.mainloop()
191

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

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

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

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