|
| 1 | +# Tkinter |
| 2 | +Tkinter es una librería de Python que permite crear interfaz gráfica de usuario (GUI en inglés). Es la librería que viene por defecto con la instalación de Python; esto no significa que otras alternativas, como por ejemplo PyQt, sean más potentes que Tkinter. |
| 3 | + |
| 4 | +## GUI con Tkinter |
| 5 | +El primer paso para crear una GUI es crear la ventana principal como se muestra en el siguiente código: |
| 6 | + |
| 7 | +```python |
| 8 | +import tkinter as tk |
| 9 | + |
| 10 | +ventana = tk.Tk() |
| 11 | +ventana.mainloop() |
| 12 | +``` |
| 13 | + |
| 14 | +El resultado es el siguiente: |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +Un programa solo puede tener una `Tk`, debido a que es la raiz del programa y el primer widget que se debe crear; por lo tanto, cerrar la `Tk` cerraría la GUI. Existen otro tipo de ventanas llamadas `Toplevel` que son ventanas de la aplicación, la diferencia con la `Tk` es que al cerrar una ventana `Toplevel` destruirá todos los widgets secundarios colocados en esa ventana pero no cerrará el programa. |
| 19 | + |
| 20 | +Una vez se tiene la ventana principal, lo siguiente es agregar los botones, entradas, labels y demás widgets que necesitemos. Para esto, Tkinter cuenta con tres mecanismos para gestionar la geometría de los widgets de nuestra GUI: los métodos `pack()`, `grid()` y `place()`. |
| 21 | + |
| 22 | +### Pack |
| 23 | +Este mecanismo para gestionar la geometría de los widgets los organiza en bloques antes de ubicarlos en el widget principal. Veamos un ejemplo simple con algunos botones: |
| 24 | + |
| 25 | +```python |
| 26 | +import tkinter as tk |
| 27 | + |
| 28 | +ventana = tk.Tk() |
| 29 | + |
| 30 | +boton1 = tk.Button(ventana, text="1") |
| 31 | +boton1.pack(side="top") |
| 32 | +boton2 = tk.Button(ventana, text="2") |
| 33 | +boton2.pack(side="top") |
| 34 | +boton3 = tk.Button(ventana, text="3") |
| 35 | +boton3.pack(side="left") |
| 36 | +boton4 = tk.Button(ventana, text="4") |
| 37 | +boton4.pack(side="bottom") |
| 38 | + |
| 39 | +ventana.mainloop() |
| 40 | +``` |
| 41 | + |
| 42 | + |
| 43 | +Se puede observar que se respeta el orden en que se "empacan" los widgets. Esto se observa claramente con los botones 1 y 2, los cuales tienen `side = "top"`, como el botón 1 está primero, se respeta y queda sobre el botón 2 que después queda sobre los widgets que se ubiquen después. Es un método muy útil cuando se va a crear una aplicación con pocos widgets. |
| 44 | + |
| 45 | +[Aquí](https://www.tutorialspoint.com/python/tk_pack.htm) para ver más sobre el método pack. |
| 46 | + |
| 47 | +### Grid |
| 48 | +Este mecanismo para gestionar la geometría de los widgets los organiza en una tabla en el widget principal. Veamos el ejemplo anterior con este método: |
| 49 | + |
| 50 | +```python |
| 51 | +import tkinter as tk |
| 52 | + |
| 53 | +ventana = tk.Tk() |
| 54 | + |
| 55 | +boton1 = tk.Button(ventana, text="1") |
| 56 | +boton1.grid(row=0, column=0) |
| 57 | +boton2 = tk.Button(ventana, text="2") |
| 58 | +boton2.grid(row=1, column=1) |
| 59 | +boton3 = tk.Button(ventana, text="3") |
| 60 | +boton3.grid(row=2, column=2) |
| 61 | +boton4 = tk.Button(ventana, text="4") |
| 62 | +boton4.grid(row=3, column=3) |
| 63 | + |
| 64 | +ventana.mainloop() |
| 65 | +``` |
| 66 | + |
| 67 | + |
| 68 | +Este método permite ubicar los widgets indicando la fila y la columna. Por defecto, el tamaño de las filas y columnas está dado por los widgets que estén ubicados en ellas, pero es posible cambiar el tamaño y combinar filas o columnas. Este método es muy útil cuando las aplicaciones contienen muchos widgets. |
| 69 | + |
| 70 | +[Aquí](https://www.tutorialspoint.com/python/tk_grid.htm) para ver más sobre el método grid. |
| 71 | + |
| 72 | +### Place |
| 73 | +Este mecanismo para gestionar la geometría de los widgets permite ubicarlos en una posición específica en el widget principal. continuando con el ejemplo anterior: |
| 74 | + |
| 75 | +```python |
| 76 | +import tkinter as tk |
| 77 | + |
| 78 | +ventana = tk.Tk() |
| 79 | + |
| 80 | +boton1 = tk.Button(ventana, text="1") |
| 81 | +boton1.place(x=0, y=0) |
| 82 | +boton2 = tk.Button(ventana, text="2") |
| 83 | +boton2.place(x=100, y=0) |
| 84 | +boton3 = tk.Button(ventana, text="3") |
| 85 | +boton3.place(x=100, y=100) |
| 86 | +boton4 = tk.Button(ventana, text="4") |
| 87 | +boton4.place(x=0, y=100) |
| 88 | + |
| 89 | +ventana.mainloop() |
| 90 | +``` |
| 91 | + |
| 92 | + |
| 93 | +Este método tiene la ventaja de que permite ubicar exactamente donde se quiere los widgets, pero hay que tener cuidado con su tamaño. Cuando son muchos widgets se vuelve problemático ubicarlos todos. |
| 94 | + |
| 95 | +[Aquí](https://www.tutorialspoint.com/python/tk_place.htm) para ver más sobre el método place. |
| 96 | + |
| 97 | +## Widgets Tkinter |
| 98 | +Tkinter cuenta con un gran número de widgets, a continuación están listados los más usados (seleccionar para ver ejemplo de cada uno): |
| 99 | +- [Button.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/button/button.md) |
| 100 | +- [Canvas.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/canvas/canvas.md) |
| 101 | +- [Checkbutton.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/checkbutton/checkbutton.md) |
| 102 | +- [ColorchooserP.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/colorchooser/colorchooser.md) |
| 103 | +- [Combobox.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/combobox/combobox.md) |
| 104 | +- [Entry.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/entry/entry.md) |
| 105 | +- [FiledialogP.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/filedialog/filedialog.md) |
| 106 | +- [Frame.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/frame/frame.md) |
| 107 | +- [Label.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/label/label.md) |
| 108 | +- [Labelframe.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/labelframe/labelframe.md) |
| 109 | +- [Listbox.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/listbox/listbox.md) |
| 110 | +- [Menu.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/menu/menu.md) |
| 111 | +- [Menubutton.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/menubutton/menubutton.md) |
| 112 | +- [Message.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/message/message.md) |
| 113 | +- [MessageboxP.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/messagebox/messagebox.md) |
| 114 | +- [Notebook.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/notebook/notebook.md) |
| 115 | +- [Panedwindow.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/panedwindow/panedwindow.md) |
| 116 | +- [Progressbar.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/progressbar/progressbar.md) |
| 117 | +- [Radiobutton.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/radiobutton/radiobutton.md) |
| 118 | +- [Scale.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/scale/scale.md) |
| 119 | +- [Scrollbar.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/scrollbar/scrollbar.md) |
| 120 | +- [Spinbox.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/spinbox/spinbox.md) |
| 121 | +- [Text.](https://github.com/juan-suarezp/PythonTkinterTutorial/blob/master/widgets/text/text.md) |
0 commit comments