0% encontró este documento útil (0 votos)
104 vistas3 páginas

Caluladora en Python Por XTN

Este documento presenta el código para crear una calculadora básica en Python usando la librería Tkinter. Importa Tkinter para la interfaz gráfica y usa funciones lambda para los métodos de los botones numéricos y operadores. Define funciones para insertar números, borrar, y evaluar operaciones, y ubica los botones en una rejilla para emular la apariencia de una calculadora tradicional.

Cargado por

CristianMarko
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como DOCX, PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
104 vistas3 páginas

Caluladora en Python Por XTN

Este documento presenta el código para crear una calculadora básica en Python usando la librería Tkinter. Importa Tkinter para la interfaz gráfica y usa funciones lambda para los métodos de los botones numéricos y operadores. Define funciones para insertar números, borrar, y evaluar operaciones, y ubica los botones en una rejilla para emular la apariencia de una calculadora tradicional.

Cargado por

CristianMarko
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como DOCX, PDF, TXT o lee en línea desde Scribd
Está en la página 1/ 3

El siguiente código fuente corresponde a la creación de una calculadora en python, se importa

la librería tkinter para la parte gráfica, así mismo se emplea la función lambda para los
métodos de entrada de los valores, espero les pueda servir de algo

from tkinter import *

ventana=Tk()

ventana.title("Calculadora")

i=0

#Entrada

e_texto = Entry(ventana, font = ("Calibri 20"))

e_texto.grid(row = 0, column = 0, columnspan = 4, padx = 5, pady = 5)

#definimos funciones:

def boton(valor):

global i

e_texto.insert(i, valor)

i += 1

def borrar():

e_texto.delete(0,END)

i=0

def operar():

ec = e_texto.get()

result = eval(ec)

e_texto.delete(0,END)

e_texto.insert(0,result)

i=0
#Botones

boton1 = Button(ventana, text = '1' , width = 5, height = 2, command = lambda: boton(1))

boton2 = Button(ventana, text = '2' , width = 5, height = 2, command = lambda: boton(2))

boton3 = Button(ventana, text = '3' , width = 5, height = 2, command = lambda: boton(3))

boton4 = Button(ventana, text = '4' , width = 5, height = 2, command = lambda: boton(4))

boton5 = Button(ventana, text = '5' , width = 5, height = 2, command = lambda: boton(5))

boton6 = Button(ventana, text = '6' , width = 5, height = 2, command = lambda: boton(6))

boton7 = Button(ventana, text = '7' , width = 5, height = 2, command = lambda: boton(7))

boton8 = Button(ventana, text = '8' , width = 5, height = 2, command = lambda: boton(8))

boton9 = Button(ventana, text = '9' , width = 5, height = 2, command = lambda: boton(9))

boton0 = Button(ventana, text = '0' , width = 5, height = 2, command = lambda: boton(0))

boton_borrar = Button(ventana, text = 'AC' , width = 5, height = 2, command = lambda:


borrar())

boton_parentesis1 = Button(ventana, text = '(' , width = 5, height = 2, command = lambda:


boton('('))

boton_parentesis2 = Button(ventana, text = ')' , width = 5, height = 2, command = lambda:


boton(')'))

boton_punto = Button(ventana, text = '.' , width = 5, height = 2, command = lambda: boton('.'))

boton_div = Button(ventana, text = '/' , width = 5, height = 2, command = lambda: boton('/'))

boton_mult = Button(ventana, text = 'x' , width = 5, height = 2, command = lambda: boton('*'))

boton_sum = Button(ventana, text = '+' , width = 5, height = 2, command = lambda:


boton('+'))

boton_rest = Button(ventana, text = '-' , width = 5, height = 2, command = lambda: boton('-'))

boton_igual = Button(ventana, text = '=' , width = 5, height = 2, command = lambda: operar())

#Agregar botonoes en pantalla

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)

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)

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)

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)

boton0.grid(row = 5, column = 0 , padx = 5, pady =5)

boton_punto.grid(row = 5, column = 1, padx = 5, pady =5)

boton_igual.grid(row = 5, column = 2, padx = 5, pady =5)

ventana.mainloop()

También podría gustarte