Utilizaremos un label para imprimir los números correspondientes a cada botón, como tkinter es un modulo orientado a eventos, debemos definir unas funciones donde se programara el evento que sucede cuando se presiona o cambia de estado el botón.
A continuación se muestra como queda la interfaz de nuestra calculadora incluyendo el label:
Al presionar algún botón se imprime el valor en el label como se observa en la figura:
Se hizo click en el botón con el numero '6' y este se imprimió en el label, igual pasa con los otros botones.
Utilizando las herramientas que tiene el tkinter para configurar los widget, es posible manipular si el texto se justifica a derecha o izquierda, el tipo de letra, el color, el color del label, etc.
A continuacion se muestra el código fuente, les recuerdo que esta escrito y ejecutado en python 3 utilizando el modulo tkinter:
import tkinter
from tkinter import *
tk = tkinter.Tk()
a=StringVar()
def asignar1(): #estas funciones capturan e imprimen los numeros en el label
num1=1
a.set(1)
def asignar2():
num3=3
a.set(2)
def asignar3():
num3=3
a.set(3)
def asignar4():
num4=4
a.set(4)
def asignar5():
num5=5
a.set(5)
def asignar6():
num6=6
a.set(6)
def asignar7():
num7=7
a.set(7)
def asignar8():
num8=8
a.set(8)
def asignar9():
num9=9
a.set(9)
def asignar0():
num0=0
a.set(0)
frame = tkinter.Frame(tk, relief=GROOVE, bd=2, height=500, width=300, bg='blue')
frame.pack(fill=BOTH,expand=20)
label = tkinter.Label(frame, text="CALCULADORA", fg='yellow', font='Arial', bg='BLUE')
#label.pack(fill=X, expand=7)
label.place(bordermode=OUTSIDE, height=30, width=200, x=50)
label2 = tkinter.Label(frame, font='Arial', bg='white', textvariable=a) #en esta linea se puede configurar totalmente las caracteristicas del label
label2.place(bordermode=OUTSIDE, height=20, width=200, y=40)
button1 = tkinter.Button(frame,text="1", command=asignar1)
button2 = tkinter.Button(frame,text="2", command=asignar2)
button3 = tkinter.Button(frame,text="3", command=asignar3)
button4 = tkinter.Button(frame,text="4", command=asignar4)
button5 = tkinter.Button(frame,text="5", command=asignar5)
button6 = tkinter.Button(frame,text="6", command=asignar6)
button7 = tkinter.Button(frame,text="7", command=asignar7)
button8 = tkinter.Button(frame,text="8", command=asignar8)
button9 = tkinter.Button(frame,text="9", command=asignar9)
button0 = tkinter.Button(frame,text="0", command=asignar0)
button1.place(bordermode=OUTSIDE, height=40, width=40, y=70)
button2.place(bordermode=OUTSIDE, height=40, width=40, y=70, x=50)
button3.place(bordermode=OUTSIDE, height=40, width=40, y=70, x=100)
button4.place(bordermode=OUTSIDE, height=40, width=40, y=120)
button5.place(bordermode=OUTSIDE, height=40, width=40, y=120, x=50)
button6.place(bordermode=OUTSIDE, height=40, width=40, y=120, x=100)
button7.place(bordermode=OUTSIDE, height=40, width=40, y=170)
button8.place(bordermode=OUTSIDE, height=40, width=40, y=170, x=50)
button9.place(bordermode=OUTSIDE, height=40, width=40, y=170, x=100)
button0.place(bordermode=OUTSIDE, height=40, width=40, y=220, x=50)
tk.mainloop()
De esta manera ya podemos imprimir los números de nuestra calculadora


No hay comentarios:
Publicar un comentario