Add library exercises

This commit is contained in:
Manuel Vergara 2024-01-19 21:34:36 +01:00
parent 658f94c62b
commit 700c489428
4 changed files with 115 additions and 0 deletions

View File

@ -0,0 +1,32 @@
#!/usr/bin/env python3
"""
Librería tkinter - pack
"""
import tkinter as tk
def accion_de_boton():
print(f"\n[+] Se ha presionado el botón")
root = tk.Tk()
root.title("Mi primera aplicación gráfica")
label1 = tk.Label(root, text="Hola mundo!", bg="red", fg="white")
label2 = tk.Label(root, text="Hola mundo!", bg="blue", fg="white")
label3 = tk.Label(root, text="Hola mundo!", bg="green", fg="white")
label1.pack(fill=tk.X)
label2.pack(fill=tk.X)
label3.pack(side=tk.LEFT, fill=tk.Y)
button1 = tk.Button(root, text="Accion",
command=accion_de_boton, bg="grey", fg="white")
button2 = tk.Button(root, text="Salir", command=root.quit,
bg="white", fg="black")
button1.pack()
button2.pack(side=tk.BOTTOM, fill=tk.X)
root.mainloop()

View File

@ -0,0 +1,19 @@
#!/usr/bin/env python3
"""
Librería tkinter - grid
"""
import tkinter as tk
root = tk.Tk()
root.title("Mi primera aplicación gráfica")
label1 = tk.Label(root, text="Hola mundo!", bg="red", fg="white")
label2 = tk.Label(root, text="Segundo Hola mundo!", bg="blue", fg="white")
label3 = tk.Label(root, text="Ola k ase", bg="green", fg="white")
label1.grid(row=0, column=0)
label2.grid(row=0, column=1)
label3.grid(row=1, column=0, columnspan=2)
root.mainloop()

View File

@ -0,0 +1,27 @@
#!/usr/bin/env python3
"""
Librería tkinter - place
geometry
"""
import tkinter as tk
def accion_de_boton():
print(f"\n[+] Se ha presionado el botón")
root = tk.Tk()
root.geometry("500x400")
root.title("Mi primera aplicación gráfica")
label1 = tk.Label(root, text="Hola mundo!", bg="red", fg="white")
label2 = tk.Label(root, text="Segundo Hola mundo!", bg="blue", fg="white")
label3 = tk.Label(root, text="Ola k ase", bg="green", fg="white")
label1.place(x=20, y=20)
label2.place(relx=0.1, rely=0.8)
label3.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
root.mainloop()

View File

@ -0,0 +1,37 @@
#!/usr/bin/env python3
"""
Librería tkinter - Entry() Widget
"""
import tkinter as tk
root = tk.Tk()
root.geometry("500x700")
root.title("Entry() Widget")
def get_data():
data = entry_widget.get()
print(f"\n[+] Datos introduccidos por el usuario:\n{data}")
def get_data_text():
data = text_widget.get("1.0", tk.END)
print(f"\n[+] Datos introduccidos por el usuario:\n{data}")
entry_widget = tk.Entry(root, width=50, borderwidth=5)
entry_widget.pack(pady=15, padx=20, fill=tk.X)
boton = tk.Button(root, text="Envia datos", command=get_data)
boton.pack(padx=20, fill=tk.X)
# Con text tengo más lineas de escritura
text_widget = tk.Text(root, width=50, borderwidth=5)
text_widget.pack(pady=15, padx=20, fill=tk.X)
boton = tk.Button(root, text="Envia datos de texto", command=get_data_text)
boton.pack(padx=20, fill=tk.X)
root.mainloop()