Está en la página 1de 6

TALLER No.

1. Ejercicio paso a paso

from tkinter import *

window = Tk()
window.title("Hello Word")
# añadimos el tamaño de la ventana
window.geometry("200x200")

lbl = Label(window, text="Hello")


# distanciamos 20 pixeles de la márgen izquierda y 30 de la superiores
lbl.grid(column=0, row=0, padx=20, pady=30)

# cambiamos el ancho y color


txt = Entry(window, width=10, background="gray")
txt.grid(column=1, row=0)

#hacemos uso de un parametro, un objeto tipo Event llamado evento


def clickON(evento):
lbl.configure(text=txt.get())

btn = Button(window, text="Click Me", background="white")


btn.grid(column=1, row=1)
#utilizamos el método bind con sus respectivos argumentos.
btn.bind("<Button-1>", clickON)

window.mainloop()

2. Ejercicio Contador creciente limitado

from tkinter import *


x=0

def sumador():
global x
if x < 20:
x += 1
else:
x=0
myLabel = Label(root, text=x)
myLabel.pack()

root = Tk()
root.title("CONTADOR")
myButton = Button(
root,
text="AUMENTO(+)",
command=sumador,
fg="gray",
background="white",
padx=140,
pady=40,
)

myButton.pack()
root.mainloop()

3. Ejercicio Calcular el factorial de cualquier número

import tkinter as tk

vent = tk.Tk()
vent.title("FAC")
vent.geometry("300x240")

def Suma(evento):
numero = factorial(int(cajaTexto1.get()))
etiqueta2.config(text=numero)

def factorial(valor):
if valor == 1 or valor == 0:
return 1
else:
return valor * factorial(valor - 1)

cajaTexto1 = tk.Entry(vent, bg="white")


cajaTexto1.place(x=70, y=30, width=140, height=35)

btn1 = tk.Button(vent, text="Calcular")


btn1.place(x=70, y=100, width=140, height=30)
btn1.bind("<Button-1>", Suma)

etiqueta2 = tk.Label(vent, text="Resultado", fg="red", background="white")


etiqueta2.place(x=70, y=170, width=140, height=30)

vent.mainloop()
4. Ejercicio Completando la interfaz de la calculadora

from tkinter import *

# Configuración ventana principal


root = Tk()
root.title("Calculadora POO")
root.resizable(0, 0)
root.geometry("400x300")

# Configuración pantalla de salida


pantalla = Entry(
root, width=22, bg="black", fg="white", borderwidth=1, font=("arial", 18, "bold")
)
pantalla.grid(row=0, padx=2, pady=2, columnspan=5, sticky="w")

# Configuración botones
boton_1 = Button(
root,
text="1",
width=9,
height=3,
bg="white",
fg="red",
borderwidth=0,
cursor="hand2",
).grid(row=1, column=0, padx=1, pady=1)

boton_2 = Button(
root,
text="2",
width=9,
height=3,
bg="white",
fg="red",
borderwidth=0,
cursor="hand2",
).grid(row=1, column=1, padx=1, pady=1)

boton_3 = Button(
root,
text="3",
width=9,
height=3,
bg="white",
fg="red",
borderwidth=0,
cursor="hand2",
).grid(row=1, column=2, padx=1, pady=1)

boton_4 = Button(
root,
text="4",
width=9,
height=3,
bg="white",
fg="red",
borderwidth=0,
cursor="hand2",
).grid(row=2, column=0, padx=1, pady=1)

boton_5 = Button(
root,
text="5",
width=9,
height=3,
bg="white",
fg="red",
borderwidth=0,
cursor="hand2",
).grid(row=2, column=1, padx=1, pady=1)

boton_6 = Button(
root,
text="6",
width=9,
height=3,
bg="white",
fg="red",
borderwidth=0,
cursor="hand2",
).grid(row=2, column=2, padx=1, pady=1)

boton_7 = Button(
root,
text="7",
width=9,
height=3,
bg="white",
fg="red",
borderwidth=0,
cursor="hand2",
).grid(row=3, column=0, padx=1, pady=1)
boton_8 = Button(
root,
text="8",
width=9,
height=3,
bg="white",
fg="red",
borderwidth=0,
cursor="hand2",
).grid(row=3, column=1, padx=1, pady=1)

boton_9 = Button(
root,
text="9",
width=9,
height=3,
bg="white",
fg="red",
borderwidth=0,
cursor="hand2",
).grid(row=3, column=2, padx=1, pady=1)

boton_igual = Button(
root,
text="=",
width=20,
height=3,
bg="red",
fg="white",
borderwidth=0,
cursor="hand2",
).grid(row=4, column=0, columnspan=2, padx=1, pady=1)

boton_punto = Button(
root,
text=".",
width=9,
height=3,
bg="spring green",
fg="black",
cursor="hand2",
borderwidth=0,
).grid(row=4, column=2, padx=1, pady=1)

boton_mas = Button(
root,
text="+",
width=9,
height=3,
bg="deep sky blue",
fg="black",
borderwidth=0,
cursor="hand2",
).grid(row=1, column=3, padx=1, pady=1)

boton_menos = Button(
root,
text="-",
width=9,
height=3,
bg="deep sky blue",
fg="black",
borderwidth=0,
cursor="hand2",
).grid(row=2, column=3, padx=1, pady=1)

boton_multiplicacion = Button(
root,
text="*",
width=9,
height=3,
bg="deep sky blue",
fg="black",
borderwidth=0,
cursor="hand2",
).grid(row=3, column=3, padx=1, pady=1)

boton_division = Button(
root,
text="/",
width=9,
height=3,
bg="deep sky blue",
fg="black",
borderwidth=0,
cursor="hand2",
).grid(row=4, column=3, padx=1, pady=1)

mainloop()

También podría gustarte