Created
June 24, 2017 02:52
-
-
Save alvesgabriel/e609411089c2595b86905e966560397e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
#-*- coding: utf-8 -*- | |
import tkinter as tk | |
class TelaCalculadora(tk.Frame): | |
"""docstring for TelaCalculadora""" | |
def __init__(self, master=None): | |
super(TelaCalculadora, self).__init__(master) | |
self.text_input = tk.StringVar() | |
self.pack() | |
self.create_widgets() | |
def create_widgets(self): | |
self.line1 = tk.Frame(self) | |
self.line1.pack(side="top") | |
self.display_enter = tk.Entry(self.line1, textvariable=self.text_input) | |
self.display_enter.pack(side="top") | |
self.line2 = tk.Frame(self) | |
self.line2.pack(side="top") | |
self.button_ce = tk.Button(self.line2, text="CE", width=6, command=self.clear) | |
self.button_ce.pack(side="left") | |
self.button_mult = tk.Button(self.line2, text="*", width=6) | |
self.button_mult["command"] = self.get_btn_mult | |
self.button_mult.pack(side="right") | |
self.line3 = tk.Frame(self) | |
self.line3.pack(side="top") | |
self.button1 = tk.Button(self.line3, text="1") | |
self.button1["command"] = self.get_btn1 | |
self.button1.pack(side="left") | |
self.button2 = tk.Button(self.line3, text="2") | |
self.button2["command"] = self.get_btn2 | |
self.button2.pack(side="left") | |
self.button3 = tk.Button(self.line3, text="3") | |
self.button3["command"] = self.get_btn3 | |
self.button3.pack(side="left") | |
self.button_div = tk.Button(self.line3, text="/") | |
self.button_div["command"] = self.get_btn_div | |
self.button_div.pack(side="left") | |
self.line4 = tk.Frame(self) | |
self.line4.pack(side="top") | |
self.button4 = tk.Button(self.line4, text="4") | |
self.button4["command"] = self.get_btn4 | |
self.button4.pack(side="left") | |
self.button5 = tk.Button(self.line4, text="5") | |
self.button5["command"] = self.get_btn5 | |
self.button5.pack(side="left") | |
self.button6 = tk.Button(self.line4, text="6") | |
self.button6["command"] = self.get_btn6 | |
self.button6.pack(side="left") | |
self.button_minus = tk.Button(self.line4, text="-") | |
self.button_minus["command"] = self.get_btn_minus | |
self.button_minus.pack(side="left") | |
self.line5 = tk.Frame(self) | |
self.line5.pack(side="top") | |
self.button7 = tk.Button(self.line5, text="7") | |
self.button7["command"] = self.get_btn7 | |
self.button7.pack(side="left") | |
self.button8 = tk.Button(self.line5, text="8") | |
self.button8["command"] = self.get_btn8 | |
self.button8.pack(side="left") | |
self.button9 = tk.Button(self.line5, text="9") | |
self.button9["command"] = self.get_btn9 | |
self.button9.pack(side="left") | |
self.button_add = tk.Button(self.line5, text="+") | |
self.button_add["command"] = self.get_btn_add | |
self.button_add.pack(side="left") | |
self.line2 = tk.Frame(self) | |
self.line2.pack(side="top") | |
self.button_eq = tk.Button(self.line2, text="=", width=6) | |
self.button_eq["command"] = self.get_btn_eq | |
self.button_eq.pack(side="right") | |
self.button0 = tk.Button(self.line2, text="0", width=3) | |
self.button0["command"] = self.get_btn0 | |
self.button0.pack(side="right") | |
def get_btn1(self): | |
self.text_input.set(self.display_enter.get() + self.button1["text"]) | |
def get_btn2(self): | |
self.text_input.set(self.display_enter.get() + self.button2["text"]) | |
def get_btn3(self): | |
self.text_input.set(self.display_enter.get() + self.button3["text"]) | |
def get_btn4(self): | |
self.text_input.set(self.display_enter.get() + self.button4["text"]) | |
def get_btn5(self): | |
self.text_input.set(self.display_enter.get() + self.button5["text"]) | |
def get_btn6(self): | |
self.text_input.set(self.display_enter.get() + self.button6["text"]) | |
def get_btn7(self): | |
self.text_input.set(self.display_enter.get() + self.button7["text"]) | |
def get_btn8(self): | |
self.text_input.set(self.display_enter.get() + self.button8["text"]) | |
def get_btn9(self): | |
self.text_input.set(self.display_enter.get() + self.button9["text"]) | |
def get_btn0(self): | |
self.text_input.set(self.display_enter.get() + self.button0["text"]) | |
def get_btn_mult(self): | |
self.text_input.set(self.display_enter.get() + self.button_mult["text"]) | |
def get_btn_div(self): | |
self.text_input.set(self.display_enter.get() + self.button_div["text"]) | |
def get_btn_minus(self): | |
self.text_input.set(self.display_enter.get() + self.button_minus["text"]) | |
def get_btn_add(self): | |
self.text_input.set(self.display_enter.get() + self.button_add["text"]) | |
def get_btn_eq(self): | |
self.text_input.set(calculadora(self.display_enter.get())) | |
def clear(self): | |
self.text_input.set("") | |
root = tk.Tk() | |
def main(): | |
# print(calculadora('5/5')) | |
app = TelaCalculadora(master=root) | |
app.mainloop() | |
def calculadora(expressao): | |
listaNumeros = obterNumeros(expressao) | |
listaOperadores = obterOperadores(expressao) | |
resultado = calcularValor(listaNumeros, listaOperadores) | |
return resultado | |
def calcularValor(listaNumeros, listaOperadores): | |
total = 0.0 | |
j = 0 | |
for i in range(len(listaNumeros)-1): | |
if total == 0.0: | |
numero1, numero2 = float(listaNumeros[i]), float(listaNumeros[i+1]) | |
operador = listaOperadores[i] | |
total = executarOperacao(numero1, operador, numero2) | |
elif total > 0.0: | |
numero2 = float(listaNumeros[i]) | |
operador = listaOperadores[j] | |
total = executarOperacao(total, operador, numero2) | |
j += 1 | |
resultado = str(total) | |
return resultado | |
def executarOperacao(numero1, operador, numero2): | |
if operador == "+": | |
resultado = numero1 + numero2 | |
elif operador == "-": | |
resultado = numero1 - numero2 | |
elif operador == "/": | |
resultado = numero1 / numero2 | |
elif operador == "*": | |
resultado = numero1 * numero2 | |
return resultado | |
def obterNumeros(expressao): | |
listaNumeros = [] | |
numeroEmString = "" | |
for exp in expressao: | |
if isOperador(exp): | |
numero = float(numeroEmString) | |
listaNumeros.append(numero) | |
numeroEmString = "" | |
else: | |
numeroEmString = numeroEmString + str(exp) | |
if numeroEmString != "" : | |
numero = float(numeroEmString) | |
listaNumeros.append(numero) | |
return listaNumeros | |
def obterOperadores(expressao): | |
listaOperadores = [] | |
for exp in expressao: | |
if isOperador(exp): | |
listaOperadores.append(exp) | |
return listaOperadores | |
def isOperador(caracter): | |
if caracter in "-+*/": | |
return True | |
return False | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment