-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24.36.py
More file actions
68 lines (59 loc) · 3.09 KB
/
24.36.py
File metadata and controls
68 lines (59 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""24.36. Скласти програму з графічним інтерфейсом для
реалізації простого калькулятора. Калькулятор повинен мати
набір кнопок для введення цифр та арифметичних дій а також
вікно для виведення результату як на рисунку нижче."""
from tkinter import *
class CalculatorGui:
def __init__(self, form):
self.equation = ''
self.frame = form
self.frame.title('Calculator')
self.def_font = ('arial', 16)
self.operators = ['C', '**', 'sqrt', '/', '*', '+', '-']
self.symbs = ['C', '^', 'sqrt', '/', 7, 8, 9, '*', 4, 5, 6, '+', 1, 2, 3, '-', 0, '=']
self.ans_label = Label(self.frame, font=self.def_font)
self.ans_label.grid(row=0, column=0, columnspan=4)
self.frame.grid_rowconfigure(0, weight=1)
for symb in self.symbs:
self.create_button(symb, 1+self.symbs.index(symb)//4, self.symbs.index(symb)%4)
def create_button(self, name, i, j):
self.symb_button = Button(self.frame, font=self.def_font, text=name, width=3,
command=lambda name=name: self.operation(name))
self.frame.grid_rowconfigure(i, weight=1)
self.frame.grid_columnconfigure(j, weight=1)
self.symb_button.grid(row=i, column=j, sticky='nsew')
if name == '=':
self.symb_button.grid(columnspan=3)
def operation(self, name):
if name == '+' and self.equation[-1] not in self.operators:
self.equation += '+'
self.ans_label.configure(text=self.equation)
elif name == '-' and self.equation[-1] not in self.operators:
self.equation += '-'
self.ans_label.configure(text=self.equation)
elif name == '*' and self.equation[-1] not in self.operators:
self.equation += '*'
self.ans_label.configure(text=self.equation)
elif name == '/' and self.equation[-1] not in self.operators:
self.equation += '/'
self.ans_label.configure(text=self.equation)
elif name == '^' and self.equation[-1] not in self.operators:
self.equation += '**'
self.ans_label.configure(text=self.equation)
elif name == 'sqrt' and self.equation[-1] not in self.operators:
self.equation += '**(1/2)'
self.ans_label.configure(text=self.equation)
elif name == 'C':
self.equation = ''
self.ans_label.configure(text=self.equation)
elif name == '=' and self.equation[-1] not in self.operators:
self.equation = str(eval(self.equation))
self.ans_label.configure(text=self.equation)
for num in range(10):
if num == name:
self.equation += str(name)
self.ans_label.configure(text=self.equation)
if __name__ == '__main__':
top = Tk()
calculator = CalculatorGui(top)
top.mainloop()