CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pytermtk

Python Terminal Toolkit for creating sophisticated text-based user interfaces with Qt-like API and full widget support

Pending
Overview
Eval results
Files

layouts.mddocs/

Layout Management

Automatic positioning and sizing of widgets using layout managers including grid layouts for complex arrangements and box layouts for linear organization.

Capabilities

Base Layout Class

TTkLayout is the abstract base class for all layout managers, providing common functionality for widget arrangement.

class TTkLayout:
    def __init__(self):
        """Initialize the base layout."""
    
    def addWidget(self, widget):
        """Add a widget to the layout."""
    
    def removeWidget(self, widget):
        """Remove a widget from the layout."""
    
    def replaceWidget(self, oldWidget, newWidget):
        """Replace one widget with another."""
    
    def addItem(self, item):
        """Add a layout item to the layout."""
    
    def itemAt(self, index):
        """Get the layout item at the specified index."""
    
    def count(self):
        """Get the number of items in the layout."""
    
    def setGeometry(self, x, y, width, height):
        """Set the geometry of the layout area."""
    
    def geometry(self):
        """Get the current geometry of the layout area."""
    
    def update(self):
        """Update the layout, repositioning all widgets."""

Layout Item Wrapper

TTkLayoutItem wraps widgets and other items for use in layouts.

class TTkLayoutItem:
    def __init__(self, widget=None, layout=None):
        """
        Initialize a layout item.
        
        Parameters:
        - widget (TTkWidget): Widget to wrap in this item
        - layout (TTkLayout): Sub-layout to wrap in this item
        """
    
    def widget(self):
        """Get the widget wrapped by this item."""
    
    def layout(self):
        """Get the layout wrapped by this item."""
    
    def setGeometry(self, x, y, width, height):
        """Set the geometry for this item."""
    
    def geometry(self):
        """Get the current geometry of this item."""
    
    def minimumSize(self):
        """Get the minimum size for this item."""
    
    def maximumSize(self):
        """Get the maximum size for this item."""

Grid Layout

TTkGridLayout arranges widgets in a grid pattern with rows and columns, allowing for complex UI arrangements.

class TTkGridLayout(TTkLayout):
    def __init__(self):
        """Initialize a grid layout."""
    
    def addWidget(self, widget, row, col, rowspan=1, colspan=1, alignment=None):
        """
        Add a widget to the grid layout.
        
        Parameters:
        - widget (TTkWidget): Widget to add
        - row (int): Grid row position (0-based)
        - col (int): Grid column position (0-based)
        - rowspan (int): Number of rows to span (default: 1)
        - colspan (int): Number of columns to span (default: 1)
        - alignment (int): Alignment within the cell
        """
    
    def addLayout(self, layout, row, col, rowspan=1, colspan=1, alignment=None):
        """
        Add a sub-layout to the grid layout.
        
        Parameters:
        - layout (TTkLayout): Layout to add
        - row (int): Grid row position
        - col (int): Grid column position
        - rowspan (int): Number of rows to span
        - colspan (int): Number of columns to span
        - alignment (int): Alignment within the cell
        """
    
    def setRowMinimumHeight(self, row, height):
        """Set minimum height for a specific row."""
    
    def setColumnMinimumWidth(self, col, width):
        """Set minimum width for a specific column."""
    
    def setRowStretch(self, row, stretch):
        """Set stretch factor for a row."""
    
    def setColumnStretch(self, col, stretch):
        """Set stretch factor for a column."""
    
    def rowMinimumHeight(self, row):
        """Get minimum height for a row."""
    
    def columnMinimumWidth(self, col):
        """Get minimum width for a column."""
    
    def rowStretch(self, row):
        """Get stretch factor for a row."""
    
    def columnStretch(self, col):
        """Get stretch factor for a column."""
    
    def rowCount(self):
        """Get the number of rows in the grid."""
    
    def columnCount(self):
        """Get the number of columns in the grid."""
    
    def setHorizontalSpacing(self, spacing):
        """Set horizontal spacing between columns."""
    
    def setVerticalSpacing(self, spacing):
        """Set vertical spacing between rows."""
    
    def horizontalSpacing(self):
        """Get horizontal spacing between columns."""
    
    def verticalSpacing(self):
        """Get vertical spacing between rows."""

Vertical Box Layout

TTkVBoxLayout arranges widgets vertically in a single column.

class TTkVBoxLayout(TTkLayout):
    def __init__(self):
        """Initialize a vertical box layout."""
    
    def addWidget(self, widget, stretch=0, alignment=None):
        """
        Add a widget to the vertical layout.
        
        Parameters:
        - widget (TTkWidget): Widget to add
        - stretch (int): Stretch factor for this widget (default: 0)
        - alignment (int): Alignment of the widget
        """
    
    def addLayout(self, layout, stretch=0):
        """
        Add a sub-layout to the vertical layout.
        
        Parameters:
        - layout (TTkLayout): Layout to add
        - stretch (int): Stretch factor for this layout
        """
    
    def addStretch(self, stretch=0):
        """
        Add stretchable space to the layout.
        
        Parameters:
        - stretch (int): Stretch factor for the space
        """
    
    def addSpacing(self, size):
        """
        Add fixed spacing to the layout.
        
        Parameters:
        - size (int): Size of the spacing in characters
        """
    
    def insertWidget(self, index, widget, stretch=0, alignment=None):
        """Insert a widget at a specific position."""
    
    def insertLayout(self, index, layout, stretch=0):
        """Insert a layout at a specific position."""
    
    def insertStretch(self, index, stretch=0):
        """Insert stretchable space at a specific position."""
    
    def insertSpacing(self, index, size):
        """Insert fixed spacing at a specific position."""
    
    def setSpacing(self, spacing):
        """Set spacing between items."""
    
    def spacing(self):
        """Get spacing between items."""
    
    def setContentsMargins(self, left, top, right, bottom):
        """Set margins around the layout content."""

Horizontal Box Layout

TTkHBoxLayout arranges widgets horizontally in a single row.

class TTkHBoxLayout(TTkLayout):
    def __init__(self):
        """Initialize a horizontal box layout."""
    
    def addWidget(self, widget, stretch=0, alignment=None):
        """
        Add a widget to the horizontal layout.
        
        Parameters:
        - widget (TTkWidget): Widget to add
        - stretch (int): Stretch factor for this widget (default: 0)
        - alignment (int): Alignment of the widget
        """
    
    def addLayout(self, layout, stretch=0):
        """
        Add a sub-layout to the horizontal layout.
        
        Parameters:
        - layout (TTkLayout): Layout to add
        - stretch (int): Stretch factor for this layout
        """
    
    def addStretch(self, stretch=0):
        """
        Add stretchable space to the layout.
        
        Parameters:
        - stretch (int): Stretch factor for the space
        """
    
    def addSpacing(self, size):
        """
        Add fixed spacing to the layout.
        
        Parameters:
        - size (int): Size of the spacing in characters
        """
    
    def insertWidget(self, index, widget, stretch=0, alignment=None):
        """Insert a widget at a specific position."""
    
    def insertLayout(self, index, layout, stretch=0):
        """Insert a layout at a specific position."""
    
    def insertStretch(self, index, stretch=0):
        """Insert stretchable space at a specific position."""
    
    def insertSpacing(self, index, size):
        """Insert fixed spacing at a specific position."""
    
    def setSpacing(self, spacing):
        """Set spacing between items."""
    
    def spacing(self):
        """Get spacing between items."""
    
    def setContentsMargins(self, left, top, right, bottom):
        """Set margins around the layout content."""

Usage Examples

Grid Layout Example

import TermTk as ttk

root = ttk.TTk()
container = ttk.TTkContainer(parent=root)

# Create a grid layout
layout = ttk.TTkGridLayout()

# Add widgets to the grid
button1 = ttk.TTkButton(text="Button 1")
button2 = ttk.TTkButton(text="Button 2") 
button3 = ttk.TTkButton(text="Button 3")
label = ttk.TTkLabel(text="Status: Ready")

# Position widgets in grid (row, col, rowspan, colspan)
layout.addWidget(button1, 0, 0)           # Row 0, Col 0
layout.addWidget(button2, 0, 1)           # Row 0, Col 1
layout.addWidget(button3, 1, 0, 1, 2)     # Row 1, spans 2 columns
layout.addWidget(label, 2, 0, 1, 2)       # Row 2, spans 2 columns

# Configure grid properties
layout.setColumnStretch(0, 1)  # Column 0 stretches
layout.setColumnStretch(1, 1)  # Column 1 stretches
layout.setRowMinimumHeight(0, 3)  # Minimum height for row 0

# Set the layout on the container
container.setLayout(layout)

root.mainloop()

Vertical Box Layout Example

import TermTk as ttk

root = ttk.TTk()
container = ttk.TTkContainer(parent=root)

# Create a vertical box layout
layout = ttk.TTkVBoxLayout()

# Add widgets vertically
title = ttk.TTkLabel(text="Application Title")
button1 = ttk.TTkButton(text="Action 1")
button2 = ttk.TTkButton(text="Action 2")
status = ttk.TTkLabel(text="Ready")

layout.addWidget(title)
layout.addStretch(1)  # Add stretchy space
layout.addWidget(button1)
layout.addSpacing(2)  # Add fixed spacing
layout.addWidget(button2)
layout.addStretch(1)  # Add stretchy space
layout.addWidget(status)

# Set spacing between items
layout.setSpacing(1)

container.setLayout(layout)
root.mainloop()

Horizontal Box Layout Example

import TermTk as ttk

root = ttk.TTk()
container = ttk.TTkContainer(parent=root)

# Create horizontal layout
layout = ttk.TTkHBoxLayout()

# Add widgets horizontally
label = ttk.TTkLabel(text="Name:")
edit = ttk.TTkLineEdit()
button = ttk.TTkButton(text="Submit")

layout.addWidget(label)
layout.addWidget(edit, stretch=1)  # Edit field stretches
layout.addWidget(button)

container.setLayout(layout)
root.mainloop()

Nested Layouts Example

import TermTk as ttk

root = ttk.TTk()
container = ttk.TTkContainer(parent=root)

# Main vertical layout
main_layout = ttk.TTkVBoxLayout()

# Header section
header = ttk.TTkLabel(text="Settings Panel")
main_layout.addWidget(header)

# Middle section with horizontal layout
middle_layout = ttk.TTkHBoxLayout()
label = ttk.TTkLabel(text="Option:")
checkbox = ttk.TTkCheckbox(text="Enable feature")
middle_layout.addWidget(label)
middle_layout.addWidget(checkbox)
middle_layout.addStretch(1)

main_layout.addLayout(middle_layout)

# Bottom section with button grid
button_layout = ttk.TTkGridLayout()
ok_btn = ttk.TTkButton(text="OK")
cancel_btn = ttk.TTkButton(text="Cancel")
apply_btn = ttk.TTkButton(text="Apply")

button_layout.addWidget(ok_btn, 0, 0)
button_layout.addWidget(cancel_btn, 0, 1)
button_layout.addWidget(apply_btn, 0, 2)

main_layout.addLayout(button_layout)

container.setLayout(main_layout)
root.mainloop()

Install with Tessl CLI

npx tessl i tessl/pypi-pytermtk

docs

color-drawing.md

container-widgets.md

core-widgets.md

display-widgets.md

event-system.md

file-dialogs.md

index.md

input-widgets.md

layouts.md

model-view.md

text-editing.md

utilities.md

tile.json