-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator_function.py
77 lines (63 loc) · 2.58 KB
/
calculator_function.py
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
69
70
71
72
73
74
75
76
77
import tkinter as tk
from typing import List, Callable
class CalculatorFunction:
""" Manages tkinter """
def __init__(
self,
root: tk.Tk,
label: tk.Label,
display: tk.Entry,
button_list: List[List[tk.Button]],
do_calculate: Callable[[str], str]
) -> None:
self.root = root
self.label = label
self.display = display
self.button_list = button_list
self.do_calculate = do_calculate
def start(self) -> None:
"""Start the gui"""
self._config_display()
self._config_buttons()
self.root.mainloop()
def _config_display(self) -> None:
"""Display configs"""
display = self.display
display.bind('<Return>', self.do_calculate)
display.bind('<KP_Enter>', self.do_calculate)
# Configurações dos botões, ações
def _config_buttons(self) -> None:
"""All button configs"""
buttons_list = self.button_list
for row in buttons_list:
for button in row:
button_text = button['text']
# Chama função para mostrar digitos no display
button.bind('<Button-1>', self.add_text_to_display)
# Clicou chama função para limpar display
if button_text == 'C':
button.bind('<Button-1>', self.clear_display)
button.config(bg='#EA4335', fg='#fff')
# Chama função para realizar os calculos
if button_text == '=':
button.bind('<Button-1>', self.calculate)
button.config(bg='#4785F4', fg='#fff')
def calculate(self, event=None) -> None:
"""Solve equations"""
equation = self.display.get()
try:
result = self.do_calculate(equation)
self.display.delete(0, 'end')
self.display.insert('end', result)
self.label.config(text=f'{equation} = {result}')
except OverflowError:
self.label.config(text='Não consegui realizar essa conta, sorry!')
except Exception:
self.label.config(text='Conta inválida')
def add_text_to_display(self, event=None) -> None:
"""Add text to display"""
self.display.insert('end', event.widget['text'].replace("DICAS (use o da direita da igualdade): |n| = abs(n) e^n = exp(n) cos() sin()", ""))
self.display.focus()
def clear_display(self, event=None) -> None:
"""Clear display"""
self.display.delete(0, 'end')