Dando continuidad a la entrada
anterior se traen dos ejemplos, el primer ejemplo imprime la frase “Hola,
Mundo”, además cuenta con un botón que borra el mensaje y el segundo ejemplo es una
calculadora.
Tercera_Ventana = Tk()
Tercera_Ventana.geometry("300x400")
Tercera_Ventana.maxsize(300,400)
Tercera_Ventana.minsize(300,400)
Tercera_Ventana.title("Tercera Ventana")
Tercera_Ventana.configure(background="gold", highlightbackground="gold", highlightcolor="black")
def Impresion():
Mensaje = tk.Message(Tercera_Ventana)
Mensaje.place(relx=0.25, rely=0.55, height=44, width=157)
Mensaje.configure(background="white", foreground="#000000", highlightbackground="#d9d9d9", highlightcolor="black", justify='center', text='''Hola, Mundo''')
return
def Limpieza():
Mensaje = tk.Message(Tercera_Ventana)
Mensaje.place(relx=0.25, rely=0.55, height=44, width=157)
Mensaje.configure(background="white", foreground="#000000", highlightbackground="#d9d9d9", highlightcolor="black", justify='center', text=''' ''')
return
Boton_Impresion = Button(Tercera_Ventana, activebackground="#ececec", activeforeground="#000000", background="#d9d9d9", disabledforeground="#a3a3a3", foreground="#000000", highlightbackground="#d9d9d9", highlightcolor="black", pady="0", text='''Imprimir''', command = Impresion).place(relx=0.25, rely=0.35, height=44, width=157)
Boton_Limpieza = Button(Tercera_Ventana, activebackground="#ececec", activeforeground="#000000", background="#d9d9d9", disabledforeground="#a3a3a3", foreground="#000000", highlightbackground="#d9d9d9", highlightcolor="black", pady="0", text='''Limpieza''', command = Limpieza).place(relx=0.25, rely=0.75, height=44, width=157)
Mensaje = tk.Message(Tercera_Ventana)
Mensaje.place(relx=0.25, rely=0.55, height=44, width=157)
Mensaje.configure(background="white", foreground="#000000", highlightbackground="#d9d9d9", highlightcolor="black", justify='center', text=''' ''')
Tercera_Ventana.mainloop()
Al ejecutar el código genera la siguiente interfaz.


Entrada.place(relx=0.04, rely=0.05, height=40, width=265)
Entrada.configure(background="white",foreground="#000000",highlightbackground="#d9d9d9",highlightcolor="black",justify='right',text=''' ''')
Las siguientes líneas utilizan
las funciones delete e incert, la primera función elimina todo
carácter que haya en la entrada, en este caso comienza desde la posición cero
hasta el final, la segunda función inserta un espacio vacío en la entrada desde
el final.
Entrada.insert(tk.END,"")
try:
Op = Entrada.get()
Resutlado = eval(Op)
Entrada.delete(0,tk.END)
Entrada.insert(tk.END,"")
Entrada.insert(tk.END,Resutlado)
except SyntaxError:
print("Introduce el número faltante")
finally:
pass
return
En la siguiente función se ingresan los dígitos y los operadores con los cuales se construyen las operaciones que se desean calcular.
def Click(Objeto):
global i
Entrada.insert(i,Objeto)
i += 1
return
def Operacion():
try:
Op = Entrada.get()
Resutlado = eval(Op)
Entrada.delete(0,tk.END)
Entrada.insert(tk.END,"")
Entrada.insert(tk.END,Resutlado)
except SyntaxError:
print("Introduce el número faltante")
finally:
pass
return
Por último se encuentra la función Limpieza la cual limpia la Entrada.
def Limpieza():
Entrada.delete(0,tk.END)
Entrada.insert(tk.END,"")
return
El código completo se muestra a continuación:
Cuarta_Ventana.geometry("290x200")
Cuarta_Ventana.maxsize(290,200)
Cuarta_Ventana.minsize(290,200)
Cuarta_Ventana.title("Calculardora 1")
Cuarta_Ventana.configure(background="green2",highlightbackground="green2",highlightcolor="black")
Entrada = tk.Entry(Cuarta_Ventana)
Entrada.place(relx=0.04, rely=0.05, height=40, width=265)
Entrada.configure(background="white",foreground="#000000",highlightbackground="#d9d9d9",highlightcolor="black",justify='right',text=''' ''')
Entrada.delete(0,tk.END)
Entrada.insert(tk.END,"")
i = 0
def Click(Objeto):
global i
Entrada.insert(i,Objeto)
i += 1
return
def Operacion():
try:
Op = Entrada.get()
Resutlado = eval(Op)
Entrada.delete(0,tk.END)
Entrada.insert(tk.END,"")
Entrada.insert(tk.END,Resutlado)
except SyntaxError:
print("Introduce el número faltante")
finally:
pass
return
def Limpieza():
Entrada.delete(0,tk.END)
Entrada.insert(tk.END,"")
return
# Numeros
Boton_1_ = Button(Cuarta_Ventana,activebackground="orange",activeforeground="#000000",background="yellow",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="yellow",highlightcolor="black",pady="0",text='''1''',command = lambda: Click(1)).place(relx=0.06, rely=0.30, height=40, width=40)
Boton_2_ = Button(Cuarta_Ventana,activebackground="orange",activeforeground="#000000",background="yellow",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="yellow",highlightcolor="black",pady="0",text='''2''',command = lambda: Click(2)).place(relx=0.21, rely=0.30, height=40, width=40)
Boton_3_ = Button(Cuarta_Ventana,activebackground="orange",activeforeground="#000000",background="yellow",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="yellow",highlightcolor="black",pady="0",text='''3''',command = lambda: Click(3)).place(relx=0.36, rely=0.30, height=40, width=40)
Boton_4_ = Button(Cuarta_Ventana,activebackground="orange",activeforeground="#000000",background="yellow",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="yellow",highlightcolor="black",pady="0",text='''4''',command = lambda: Click(4)).place(relx=0.06, rely=0.52, height=40, width=40)
Boton_5_ = Button(Cuarta_Ventana,activebackground="orange",activeforeground="#000000",background="yellow",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="yellow",highlightcolor="black",pady="0",text='''5''',command = lambda: Click(5)).place(relx=0.21, rely=0.52, height=40, width=40)
Boton_6_ = Button(Cuarta_Ventana,activebackground="orange",activeforeground="#000000",background="yellow",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="yellow",highlightcolor="black",pady="0",text='''6''',command = lambda: Click(6)).place(relx=0.36, rely=0.52, height=40, width=40)
Boton_7_ = Button(Cuarta_Ventana,activebackground="orange",activeforeground="#000000",background="yellow",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="yellow",highlightcolor="black",pady="0",text='''7''',command = lambda: Click(7)).place(relx=0.06, rely=0.74, height=40, width=40)
Boton_8_ = Button(Cuarta_Ventana,activebackground="orange",activeforeground="#000000",background="yellow",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="yellow",highlightcolor="black",pady="0",text='''8''',command = lambda: Click(8)).place(relx=0.21, rely=0.74, height=40, width=40)
Boton_9_ = Button(Cuarta_Ventana,activebackground="orange",activeforeground="#000000",background="yellow",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="yellow",highlightcolor="black",pady="0",text='''9''',command = lambda: Click(9)).place(relx=0.36, rely=0.74, height=40, width=40)
Boton_0_ = Button(Cuarta_Ventana,activebackground="orange",activeforeground="#000000",background="yellow",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="yellow",highlightcolor="black",pady="0",text='''0''',command = lambda: Click(0)).place(relx=0.51, rely=0.74, height=40, width=40)
# Operaciones
Boton_suma_ = Button(Cuarta_Ventana,activebackground="pink",activeforeground="#000000",background="salmon",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="salmon",highlightcolor="black",pady="0",text='''+''', command =lambda: Click("+")).place(relx=0.51, rely=0.30, height=40, width=40)
Boton_resta_ = Button(Cuarta_Ventana,activebackground="pink",activeforeground="#000000",background="salmon",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="salmon",highlightcolor="black",pady="0",text='''-''', command =lambda: Click("-")).place(relx=0.51, rely=0.52, height=40, width=40)
Boton_multi_ = Button(Cuarta_Ventana,activebackground="pink",activeforeground="#000000",background="salmon",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="salmon",highlightcolor="black",pady="0",text='''*''', command =lambda: Click("*")).place(relx=0.66, rely=0.30, height=40, width=40)
Boton_divic_ = Button(Cuarta_Ventana,activebackground="#ece",activeforeground="#000000",background="salmon",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="salmon",highlightcolor="black",pady="0",text='''/''', command =lambda: Click("/")).place(relx=0.66, rely=0.52, height=40, width=40)
Boton_C_ = Button(Cuarta_Ventana,activebackground="pink",activeforeground="#000000",background="salmon",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="salmon",highlightcolor="black",pady="0",text='''C''', command =Limpieza).place(relx=0.66, rely=0.74, height=40, width=40)
Boton_1_ = Button(Cuarta_Ventana,activebackground="pink",activeforeground="#000000",background="salmon",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="salmon",highlightcolor="black",pady="0",text='''(''', command = lambda : Click("(")).place(relx=0.81, rely=0.52, height=40, width=40)
Boton_2_ = Button(Cuarta_Ventana,activebackground="pink",activeforeground="#000000",background="salmon",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="salmon",highlightcolor="black",pady="0",text=''')''', command = lambda : Click(")")).place(relx=0.81, rely=0.30, height=40, width=40)
Boton_igual_ = Button(Cuarta_Ventana,activebackground="#ececec",activeforeground="#000000",background="DodgerBlue2",disabledforeground="#a3a3a3",foreground="#000000",highlightbackground="DodgerBlue2",highlightcolor="black",pady="0",text='''=''', command =lambda: Operacion()).place(relx=0.81, rely=0.74, height=40, width=40)
Cuarta_Ventana.mainloop()

No hay comentarios.:
Publicar un comentario