├── README.md └── main.py /README.md: -------------------------------------------------------------------------------- 1 | # Calculadora-Python 2 | Calculadora implementada em Python, usando Tkinter/ calculator implemented in Python, using Tkinter 3 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from tkinter import* 2 | import itertools 3 | import math 4 | 5 | class Calculadora(): 6 | def __init__(self, raiz): 7 | 8 | self.input_ = "0" 9 | self.soma = Celula() 10 | caixa_texto = Texto(raiz, self.input_) 11 | 12 | def novo_botao(text, row, column, function): 13 | return Botao(raiz, text, row, column, function) 14 | 15 | def input_numero(numero): 16 | return lambda : self.Numero(numero) 17 | 18 | buttons = {} 19 | 20 | numbers_positions = list(itertools.product([3, 4, 5], [2, 3, 4])) 21 | for numero in range(1, 10): 22 | buttons.update({str(numero) : (*numbers_positions[numero-1], input_numero(numero))}) 23 | 24 | buttons.update({"0": (5, 5, input_numero(0))}) 25 | 26 | buttons.update({ 27 | "pi()": (2, 1, self.Pi), 28 | "x²": (2, 2, self.Pow), 29 | "log10()": (2, 3, self.Log10), 30 | "%": (2, 4, self.Percentual), 31 | "raiz()": (2, 5, self.Raiz_Quadrada), 32 | "clear": (2, 6, self.clear_), 33 | "sen()": (3, 1, self.Seno), 34 | "cos()": (4, 1, self.Cosseno), 35 | "tg()": (5, 1, self.Tangente) 36 | }) 37 | 38 | for button in buttons.keys(): 39 | novo_botao(button, *buttons[button]) 40 | 41 | 42 | def clear_(self): 43 | self.input_ = "0" 44 | caixa_texto = Texto(raiz, self.input_) 45 | 46 | def Raiz_Quadrada(self): 47 | temporario = math.sqrt(float(self.input_)) 48 | self.input_ = str(temporario) 49 | caixa_texto = Texto(raiz, self.input_) 50 | 51 | def Seno(self): 52 | temporario = ((int(self.input_))*2*math.pi)/(360) 53 | self.input_ = math.sin(temporario) 54 | caixa_texto = Texto(raiz, self.input_) 55 | 56 | def Cosseno(self): 57 | temporario = ((int(self.input_))*2*math.pi)/(360) 58 | self.input_ = math.cos(temporario) 59 | caixa_texto = Texto(raiz, self.input_) 60 | 61 | def Tangente(self): 62 | temporario = ((int(self.input_))*2*math.pi)/(360) 63 | self.input_ = math.tan(temporario) 64 | caixa_texto = Texto(raiz, self.input_) 65 | 66 | def Log10(self): 67 | temporario = math.log10(int(self.input_)) 68 | self.input_ = temporario 69 | caixa_texto = Texto(raiz, self.input_) 70 | 71 | def Pi(self): 72 | self.input_ = math.pi 73 | caixa_texto = Texto(raiz, self.input_) 74 | 75 | def Pow(self): 76 | self.input_ = math.pow(float(self.input_), 2) 77 | caixa_texto = Texto(raiz, self.input_) 78 | 79 | def Percentual(self): 80 | self.input_ = (float(self.input_)/100) 81 | caixa_texto = Texto(raiz, self.input_) 82 | 83 | def Igual(self): 84 | self.soma._numero_B = self.input_ 85 | soma = self.soma 86 | operacao = {"soma": soma.soma, 87 | "subtracao": soma.subtracao, 88 | "multiplicacao": soma.multiplicacao, 89 | "divisao": soma.divisao} 90 | v = operacao() 91 | caixa_texto = Texto(raiz, v) 92 | self.input_ = v 93 | 94 | def Soma(self): 95 | self.soma._numero_A = self.input_ 96 | self.soma._sinal = "soma" 97 | self.input_ = "0" 98 | 99 | def Multiplicacao(self): 100 | self.soma._numero_A = self.input_ 101 | self.soma._sinal = "multiplicacao" 102 | self.input_ = "0" 103 | 104 | def Divisao(self): 105 | self.soma._numero_A = self.input_ 106 | self.soma._sinal = "divisao" 107 | self.input_ = "0" 108 | print(self.soma._numero_A ) 109 | 110 | def Subtracao(self): 111 | self.soma._numero_A = self.input_ 112 | self.soma._sinal = "subtracao" 113 | self.input_ = "0" 114 | 115 | def Numero(self, numero): 116 | self.input_ += str(numero) 117 | caixa_texto = Texto(raiz, self.input_) 118 | 119 | 120 | 121 | class Botao(): 122 | def __init__(self, frame, text_botao, linha, coluna, comando): 123 | self.button = Button(frame, text = text_botao, fg="black", bg="grey", command = comando, font = ("Arial", "20", "bold")) 124 | self.button["width"] = 5 125 | self.button["height"] = 1 126 | self.button.grid(row = linha, column = coluna) 127 | 128 | class Celula(): 129 | def __init__(self): 130 | self._numero_A = None 131 | self._numero_B = None 132 | self._sinal = None 133 | 134 | def soma(self): 135 | return float(self._numero_A) + float(self._numero_B) 136 | 137 | def subtracao(self): 138 | return float(self._numero_A) - float(self._numero_B) 139 | 140 | def multiplicacao(self): 141 | return float(self._numero_A) * float(self._numero_B) 142 | 143 | def divisao(self): 144 | return float(self._numero_A) / float(self._numero_B) 145 | 146 | class Texto(): 147 | def __init__(self, raiz, texto): 148 | self.texto = Label(raiz, text =round (float(texto), 5), font = ("Arial", "72", "bold")) 149 | self.texto["height"] = 2 150 | self.texto["width"] = 2 151 | self.texto.grid(row=1, column=1, columnspan=6,sticky=W+E+N+S) 152 | 153 | 154 | 155 | raiz = Tk() 156 | raiz.title("Calculadora") 157 | raiz.geometry("570x452") 158 | m = Calculadora(raiz) 159 | raiz.mainloop() 160 | --------------------------------------------------------------------------------