Such an interface may be easier to describe from the inside out, though, rather than from the outside in. For example, the GUI shown in Figure 1 could be described as:
group_3element then describes the entire GUI, including the nested hierarchy of other elements.
TkLayoutPython package allows GUIs to be built by describing them in just this way. The key to this process is separation of the steps of describing the interface and creating the interface.
To describe the interface, the GUI implementer must first assign a name to each element of the GUI. For the example shown in Figure 1, reasonable names would be "topic_selection", "date_selection", etc. The interface is then described using methods of an
AppLayoutobject as follows:
column_elementsmethods synthesize and return new element names that identify the element groupings that are created—i.e.,
group_3. After the structure of the GUI has been described, the frames that implement that structure are created with another method of the
AppLayoutobject, as follows:
rootargument is a Tkinter top level object or other container that will contain the GUI.
create_layoutmethod creates a series of nested frames, as shown in Figure 2 for the GUI illustrated in Figure 1.
After this set of frames is created, the enclosing frame for each element can be obtained from the
AppLayoutobject using that element's name. The GUI implementer can then populate each of these frames with other GUI widgets as appropriate. The
AppLayoutclass will also take a dictionary of element names and element-construction functions, and run those functions to populate each element's enclosing frame. The functions that create the elements for each frame require only the enclosing frame object, and do not need to know anything about how the elements are laid out in relation to one another.
Separation of structure description and implementation in this way allows the GUI construction process to be greatly simplified. Exploration of alternate interface layouts is also simplified: each alternate layout can be described in just a few lines of code, without any changes required to code to populate or arrange the interface elements.
The TkLayout package can be downloaded from the Python Package Index (PyPI) or installed with the command
The documentation is available on ReadTheDocs.