16-02-2019, 12:51 AM
Tengo una calculadora hecha en Tkinter. La misma realiza las operaciones básicas (Sumar,restar, multiplicar, dividir). Pero todo presionando los botones de la interfaz. Quiero saber como puedo incluirle los binds, para introducir los números al presionar en el teclado, asi como también cuando presione return haga el cálculo con las operaciones que he indicado. Aquí les dejo el código, espero puedan ayudarme u.u. De corazón, Muchas gracias
Código:
import sys
from tkinter import *
#funciones de operación
def btnClick(numbers):
global operator
operator = operator + str(numbers)
num1.set(operator)
def btnClear():
global operator
operator = ""
num1.set(operator)
def btnEquals():
global operator
sumup = str(eval(operator))
num1.set(sumup)
operator = ""
micolor = '#d3d3d3'
#Frame principal
root = Tk()
frame = Frame(root, width=20)
frame.pack()
#Titulo Calculadora
root.title('Calculadora')
#operador
operator=""
#Campo de Texto
num1 = StringVar()
topframe = Frame(root)
topframe.pack(side = TOP)
txtDisplay = Entry(frame,textvariable = num1, bd=5, insertwidth=1, font=30, justify="right")
txtDisplay.focus()
txtDisplay.pack(side = TOP)
#Botones
boton1 = Button(topframe,padx=10,pady=10,bd=3,text="1",fg="black", command = lambda:btnClick(1))
boton1.pack(side = LEFT)
boton2 = Button(topframe,padx=10,pady=10,bd=3,text="2",fg="black", command = lambda:btnClick(2))
boton2.pack(side = LEFT)
boton3 = Button(topframe,padx=10,pady=10,bd=3,text="3",fg="black", command = lambda:btnClick(3))
boton3.pack(side = LEFT)
boton4 = Button(topframe,padx=10,pady=10,bd=3,text="4",fg="black", command = lambda:btnClick(4))
boton4.pack(side = LEFT)
frame2 = Frame(root)
frame2.pack(side = TOP)
#Botones
boton1 = Button(frame2,padx=10,pady=10,bd=3,text="5",fg="black", command = lambda:btnClick(5))
boton1.pack(side = LEFT)
boton2 = Button(frame2,padx=10,pady=10,bd=3,text="6",fg="black", command = lambda:btnClick(6))
boton2.pack(side = LEFT)
boton3 = Button(frame2,padx=10,pady=10,bd=3,text="7",fg="black", command = lambda:btnClick(7))
boton3.pack(side = LEFT)
boton4 = Button(frame2,padx=10,pady=10,bd=3,text="8",fg="black", command = lambda:btnClick(8))
boton4.pack(side = LEFT)
frame3 = Frame(root)
frame3.pack(side = TOP)
boton5 = Button(frame3,padx=10,pady=10,bd=3,text="9",fg="black", command = lambda:btnClick(9))
boton5.pack(side = LEFT)
boton6 = Button(frame3,padx=10,pady=10,bd=3,text="0",fg="black", command = lambda:btnClick(0))
boton6.pack(side = LEFT)
boton5 = Button(frame3,padx=10,pady=10,bd=3,text="C",fg="black",command= btnClear)
boton5.pack(side = LEFT)
boton6 = Button(frame3,padx=10,pady=10,bd=3,text="=",fg="black", command = btnEquals)
boton6.pack(side = LEFT)
frame4 = Frame(root)
frame4.pack(side = TOP)
boton5 = Button(frame4,padx=10,pady=10,bd=3,text="*",fg="black", command = lambda:btnClick("*"))
boton5.pack(side = LEFT)
boton6 = Button(frame4,padx=10,pady=10,bd=3,text="/",fg="black", command = lambda:btnClick("/"))
boton6.pack(side = LEFT)
boton5 = Button(frame4,padx=10,pady=10,bd=3,text="-",fg="black", command = lambda:btnClick("-"))
boton5.pack(side = LEFT)
boton6 = Button(frame4,padx=10,pady=10,bd=3,text="+",fg="black", command = lambda:btnClick("+"))
boton6.pack(side = LEFT)
root.mainloop()