Widgets del core de tkinter

tkinter tiene 22 widgets1. La consola interactiva de Python nos los muestra si se lo pedimos con import tkinter; dir(tkinter) o con import tkinter; help(tkinter).

BitmapImage Button Canvas Checkbutton Entry Frame
Image Label LabelFrame Listbox Menu Menubutton
Message OptionMenu PanedWindow PhotoImage Radiobutton Scale
Scrollbar Spinbox Text Toplevel

Algunos de ellos se sustituyen en tkinter.ttk por otros basados en temas o estilos2.

Button Checkbutton Entry Frame
Label LabelFrame Menubutton OptionMenu
PanedWindow Radiobutton Scale Scrollbar

tkinter.ttk añade 6 widgets a la colección de tkinter.

Combobox LabeledScale Notebook
Progressbar Separator TreeView

Todos los widgets son ventanas, con muchos atributos y métodos en común. A lo largo del curso los utilizaremos construyendo pequeñas aplicaciones con las que podamos observar el control que tenemos sobre cada uno de ellos.

tkinter.ttk también añade la clase Style para gestionar la apariencia de sus widgets de un modo más genérico que el usado por tkinter.

Esta información la puedes obtener escribiendo import tkinter.ttk; dir(tkinter.ttk) o import tkinter.ttk; help(tkinter.ttk) en la consola interactiva de Python.

Jerarquía de clases en tkinter

Escribiendo import tkinter as tk; help(tk) en la consola interactiva de Python nos informa de que tkinter es una envoltura (wrapper)

Nos remite a la documentación oficial, donde especifica

The tkinter package (“Tk interface”) is the standard Python interface to the Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, as well as on Windows systems. (Tk itself is not part of Python; it is maintained at ActiveState.) You can check that tkinter is properly installed on your system by running python -m tkinter from the command line; this should open a window demonstrating a simple Tk interface.

También obtenemos una visión general del módulo tkinter, con ejemplo incluido:

DESCRIPTION
    Tkinter provides classes which allow the display, positioning and
    control of widgets. Toplevel widgets are Tk and Toplevel. Other
    widgets are Frame, Label, Entry, Text, Canvas, Button, Radiobutton,
    Checkbutton, Scale, Listbox, Scrollbar, OptionMenu, Spinbox
    LabelFrame and PanedWindow.

    Properties of the widgets are specified with keyword arguments.
    Keyword arguments have the same name as the corresponding resource
    under Tk.

    Widgets are positioned with one of the geometry managers Place, Pack
    or Grid. These managers can be called with methods place, pack, grid
    available in every Widget.

    Actions are bound to events by resources (e.g. keyword argument
    command) or with the method bind.

    Example (Hello, World):
    import tkinter
    from tkinter.constants import *
    tk = tkinter.Tk()
    frame = tkinter.Frame(tk, relief=RIDGE, borderwidth=2)
    frame.pack(fill=BOTH,expand=1)
    label = tkinter.Label(frame, text="Hello, World")
    label.pack(fill=X, expand=1)
    button = tkinter.Button(frame,text="Exit",command=tk.destroy)
    button.pack(side=BOTTOM)
    tk.mainloop()

El listado de los paquetes que incluye:

PACKAGE CONTENTS
    __main__
    colorchooser
    commondialog
    constants
    dialog
    dnd
    filedialog
    font
    messagebox
    scrolledtext
    simpledialog
    tix
    ttk

Y la estructura jerárquica de las clases que contiene:

CLASSES
    builtins.object
        CallWrapper
        Event
        Grid
        Image
            BitmapImage
            PhotoImage
        Misc
            BaseWidget
                Toplevel(BaseWidget, Wm)
                Widget(BaseWidget, Pack, Place, Grid)
                    Button
                    Canvas(Widget, XView, YView)
                    Checkbutton
                    Entry(Widget, XView)
                    Frame
                    Label
                    LabelFrame
                    Listbox(Widget, XView, YView)
                    Menu
                    Menubutton
                        OptionMenu
                    Message
                    PanedWindow
                    Radiobutton
                    Scale
                    Scrollbar
                    Spinbox(Widget, XView)
                    Text(Widget, XView, YView)
            Tk(Misc, Wm)
        Pack
        Place
        Variable
            BooleanVar
            DoubleVar
            IntVar
            StringVar
        Wm
        XView
        YView
    builtins.str(builtins.object)
        EventType(builtins.str, enum.Enum)
    enum.Enum(builtins.object)
        EventType(builtins.str, enum.Enum)

Además de incorporar ayuda para cada una de sus clases.

Jerarquía de clases en tkinter.ttk

Escribiendo import tkinter.ttk as ttk; help(ttk) en la consola interactiva de Python obtenemos una visión general del módulo tkinter.ttk, aclarando un concepto básico de la programación visual, la separación del código que implementa el comportamiento de un widget del que implementa su apariencia:

DESCRIPTION
    This module provides classes to allow using Tk themed widget set.

    Ttk is based on a revised and enhanced version of
    TIP #48 (http://tip.tcl.tk/48) specified style engine.

    Its basic idea is to separate, to the extent possible, the code
    implementing a widget's behavior from the code implementing its
    appearance. Widget class bindings are primarily responsible for
    maintaining the widget state and invoking callbacks, all aspects
    of the widgets appearance lies at Themes.

La jerarquía de clases es:

CLASSES
    builtins.object
        Style
    tkinter.Entry(tkinter.Widget, tkinter.XView)
        Entry(Widget, tkinter.Entry)
            Combobox
    tkinter.PanedWindow(tkinter.Widget)
        Panedwindow(Widget, tkinter.PanedWindow)
    tkinter.Scale(tkinter.Widget)
        Scale(Widget, tkinter.Scale)
    tkinter.Scrollbar(tkinter.Widget)
        Scrollbar(Widget, tkinter.Scrollbar)
    tkinter.XView(builtins.object)
        Treeview(Widget, tkinter.XView, tkinter.YView)
    tkinter.YView(builtins.object)
        Treeview(Widget, tkinter.XView, tkinter.YView)
    Widget(tkinter.Widget)
        Button
        Checkbutton
        Entry(Widget, tkinter.Entry)
            Combobox
        Frame
            LabeledScale
        Label
        Labelframe
        Menubutton
            OptionMenu
        Notebook
        Panedwindow(Widget, tkinter.PanedWindow)
        Progressbar
        Radiobutton
        Scale(Widget, tkinter.Scale)
        Scrollbar(Widget, tkinter.Scrollbar)
        Separator
        Sizegrip
        Treeview(Widget, tkinter.XView, tkinter.YView)

Referencias

1. El widget Menubutton de tkinter es obsoleto desde la versión Tk8.0.
2. Esto se traduce en que su aspecto no se controla de forma individual, se gestiona a través de objetos dedicados a controlar el estilo. Los ejemplos de código que usaremos pueden referirse a widgets de tkinter o de tkinter.ttk, presta especial atención a este detalle porque no se configuran con la misma orden el widget tkinter.Label que el widget tkinter.ttk.Label, por ejemplo.

results matching ""

    No results matching ""