-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.py
More file actions
79 lines (68 loc) · 4.05 KB
/
Calculator.py
File metadata and controls
79 lines (68 loc) · 4.05 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
69
70
71
72
73
74
75
76
77
78
79
#Small calculator done with TKinter & Python
from tkinter import *
ventana = Tk() #mainScreen
ventana.title("Calculadora") #mainScreen title
e_texto = Entry(ventana, font = ("Calibri 20")) #text entry & Letter & size
e_texto.grid(row = 0, column =0, columnspan =4, padx = 10, pady = 10) #adding it and place it
i = 0
#functions
#Con esta funcion cuando pulsamos un boton añade la imagen en la ventana(E_texto)
def click_boton(valor):
global i
e_texto.insert(i, valor)
i += 1 #Con esta variable haremos que cuando pulsemos un numero se coloque bien
def borrar(): #En la ventana del texto ejecutamos la función borrar y inicializamos a 0 el indice
e_texto.delete(0,END)
i = 0
def hacer_operacion():
ecuacion = e_texto.get() #Guardamos el texto en la var ecuacion
resultado = eval(ecuacion) #Evaluamos y guardamos el resultado en la var
e_texto.delete(0, END) #Borramos texto
e_texto.insert(0,resultado) #añadimos el resultado ya calculado
i = 0 #finalmente inicializamos el indice
#making buttons
boton1 = Button(ventana, text ="1", width =5, height =2, command = lambda: click_boton(1))
boton2 = Button(ventana, text ="2", width =5, height =2, command = lambda: click_boton(2))
boton3 = Button(ventana, text ="3", width =5, height =2, command = lambda: click_boton(3))
boton4 = Button(ventana, text ="4", width =5, height =2, command = lambda: click_boton(4))
boton5 = Button(ventana, text ="5", width =5, height =2, command = lambda: click_boton(5))
boton6 = Button(ventana, text ="6", width =5, height =2, command = lambda: click_boton(6))
boton7 = Button(ventana, text ="7", width =5, height =2, command = lambda: click_boton(7))
boton8 = Button(ventana, text ="8", width =5, height =2, command = lambda: click_boton(8))
boton9 = Button(ventana, text ="9", width =5, height =2, command = lambda: click_boton(9))
boton0 = Button(ventana, text ="0", width =13, height =2, command = lambda: click_boton(0))
boton_borrar = Button(ventana, text = chr(9003), width =5, height =2, command = lambda: borrar())
boton_parentesis1 = Button(ventana, text ="(", width =5, height =2, command = lambda: click_boton("("))
boton_parentesis2 = Button(ventana, text =")", width =5, height =2, command = lambda: click_boton(")"))
boton_punto = Button(ventana, text =".", width =5, height =2, command = lambda: click_boton("."))
boton_div = Button(ventana, text =chr(247), width =5, height =2, command = lambda: click_boton("/"))
boton_mult = Button(ventana, text ="x", width =5, height =2, command = lambda: click_boton("*"))
boton_sum = Button(ventana, text ="+", width =5, height =2, command = lambda: click_boton("+"))
boton_rest = Button(ventana, text ="-", width =5, height =2, command = lambda: click_boton("-"))
boton_igual = Button(ventana, text ="=", width =5, height =2, command = lambda: hacer_operacion())
#adding buttons into mainScreen
#First line
boton_borrar.grid(row = 1,column = 0, padx = 5, pady =5)
boton_parentesis1.grid(row = 1,column = 1, padx = 5, pady =5)
boton_parentesis2.grid(row = 1,column = 2, padx = 5, pady =5)
boton_div.grid(row = 1,column = 3, padx = 5, pady =5)
#Second line
boton7.grid(row = 2,column = 0, padx = 5, pady =5)
boton8.grid(row = 2,column = 1, padx = 5, pady =5)
boton9.grid(row = 2,column = 2, padx = 5, pady =5)
boton_mult.grid(row = 2,column = 3, padx = 5, pady =5)
#Third line
boton4.grid(row = 3,column = 0, padx = 5, pady =5)
boton5.grid(row = 3,column = 1, padx = 5, pady =5)
boton6.grid(row = 3,column = 2, padx = 5, pady =5)
boton_sum.grid(row = 3,column = 3, padx = 5, pady =5)
#Fourth line
boton1.grid(row = 4,column = 0, padx = 5, pady =5)
boton2.grid(row = 4,column = 1, padx = 5, pady =5)
boton3.grid(row = 4,column = 2, padx = 5, pady =5)
boton_rest.grid(row = 4,column = 3, padx = 5, pady =5)
#Fifth line
boton0.grid(row = 5,column = 0, columnspan = 2, padx = 5, pady =5)
boton_punto.grid(row = 5,column = 2, padx = 5, pady =5)
boton_igual.grid(row = 5,column = 3, padx = 5, pady =5)
ventana.mainloop()