Tkinter
Tkinter en Python
Section titled “Tkinter en Python”¿Qué es Tkinter?
Section titled “¿Qué es Tkinter?”Tkinter es la biblioteca nativa de Python para interfaces gráficas (GUI).
Permite crear ventanas, botones, menús, formularios, cuadros de texto, y mucho más. Viene preinstalada con Python (no requiere pip install).
Primer ejemplo: ventana básica
Section titled “Primer ejemplo: ventana básica”import tkinter as tk
ventana = tk.Tk() # Crea la ventana principalventana.title("Mi primera GUI") # Títuloventana.geometry("400x300") # Tamaño (ancho x alto)ventana.mainloop() # Inicia el bucle principal🔹 Tk() → crea la aplicación. 🔹 mainloop() → mantiene la ventana abierta escuchando eventos (clics, teclas, etc.).
Widgets principales
Section titled “Widgets principales”Los widgets son los componentes visuales de Tkinter.
Ejemplo con varios widgets
Section titled “Ejemplo con varios widgets”import tkinter as tk
def saludar():etiqueta.config(text=f"Hola, {entrada.get()}!")
ventana = tk.Tk()ventana.title("Ejemplo Widgets")
etiqueta = tk.Label(ventana, text="Escribe tu nombre:")etiqueta.pack()
entrada = tk.Entry(ventana)entrada.pack()
boton = tk.Button(ventana, text="Saludar", command=saludar)boton.pack()
ventana.mainloop()Gestores de diseño
Section titled “Gestores de diseño”Tkinter tiene tres formas principales de organizar widgets:
Ejemplo
Section titled “Ejemplo”etiqueta1.grid(row=0, column=0)entrada1.grid(row=0, column=1)boton.grid(row=1, column=0, columnspan=2)Eventos y comandos
Section titled “Eventos y comandos”Puedes ejecutar funciones al hacer clic o escribir algo.
def al_hacer_click():print("¡Botón presionado!")
boton = tk.Button(ventana, text="Haz clic", command=al_hacer_click)También puedes manejar eventos con .bind():
def tecla_presionada(evento):print("Tecla:", evento.char)
ventana.bind("", tecla_presionada)Frames y organización
Section titled “Frames y organización”Los Frames permiten dividir la interfaz en secciones.
frame_superior = tk.Frame(ventana)frame_superior.pack()
tk.Label(frame_superior, text="Arriba").pack()tk.Label(ventana, text="Abajo").pack()Widgets más avanzados
Section titled “Widgets más avanzados”Checkbutton
Section titled “Checkbutton”var = tk.BooleanVar()tk.Checkbutton(ventana, text="Acepto términos", variable=var).pack()Radiobutton
Section titled “Radiobutton”opcion = tk.StringVar(value="A")tk.Radiobutton(ventana, text="Opción A", variable=opcion, value="A").pack()tk.Radiobutton(ventana, text="Opción B", variable=opcion, value="B").pack()Listbox
Section titled “Listbox”lista = tk.Listbox(ventana)for i in ["Python", "C++", "Java"]:lista.insert(tk.END, i)lista.pack()Colores y fuentes
Section titled “Colores y fuentes”tk.Label(ventana, text="Texto colorido", fg="white", bg="blue", font=("Arial", 14, "bold")).pack()Imágenes
Section titled “Imágenes”Tkinter soporta imágenes con PhotoImage (formato .png, .gif).
img = tk.PhotoImage(file="imagen.png")tk.Label(ventana, image=img).pack()ventana.mainloop()menu = tk.Menu(ventana)ventana.config(menu=menu)
submenu = tk.Menu(menu, tearoff=0)menu.add_cascade(label="Archivo", menu=submenu)submenu.add_command(label="Nuevo")submenu.add_command(label="Salir", command=ventana.quit)Ventanas emergentes
Section titled “Ventanas emergentes”from tkinter import messageboxmessagebox.showinfo("Título", "Hola desde una ventana emergente")Ejemplo completo
Section titled “Ejemplo completo”import tkinter as tkfrom tkinter import messagebox
def saludar():nombre = entrada.get()if nombre:messagebox.showinfo("Saludo", f"Hola, {nombre}!")else:messagebox.showwarning("Advertencia", "Debes ingresar un nombre")
ventana = tk.Tk()ventana.title("App Completa")ventana.geometry("300x200")
tk.Label(ventana, text="Tu nombre:").pack(pady=5)entrada = tk.Entry(ventana)entrada.pack(pady=5)tk.Button(ventana, text="Saludar", command=saludar).pack(pady=10)
ventana.mainloop()