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

container-widgets.mddocs/

Container Widgets

Specialized container widgets including windows, frames, splitters, and scrollable areas for organizing and structuring complex UIs.

Capabilities

Window Widget

TTkWindow provides a top-level container with title bar, borders, and window controls.

class TTkWindow(TTkContainer):
    def __init__(self, parent=None, **kwargs):
        """
        Initialize a window widget.
        
        Parameters:
        - title (str): Window title
        - pos (TTkPoint): Window position
        - size (TTkSize): Window size
        - flags (int): Window flags for behavior
        - border (bool): Show window border (default: True)
        """
    
    def setTitle(self, title):
        """Set the window title."""
    
    def title(self):
        """Get the window title."""
    
    def setFlags(self, flags):
        """Set window flags."""
    
    def flags(self):
        """Get window flags."""
    
    def setBorder(self, border):
        """Enable/disable window border."""
    
    def border(self):
        """Check if border is enabled."""
    
    def close(self):
        """Close the window."""
    
    def showMaximized(self):
        """Show window maximized."""
    
    def showNormal(self):
        """Show window in normal state."""
    
    def isMaximized(self):
        """Check if window is maximized."""
    
    def setResizable(self, resizable):
        """Enable/disable window resizing."""
    
    def isResizable(self):
        """Check if window is resizable."""
    
    def setModal(self, modal):
        """Set window modality."""
    
    def isModal(self):
        """Check if window is modal."""
    
    # Signals
    closed: pyTTkSignal  # Emitted when window is closed

Frame Widget

TTkFrame provides a container with customizable borders and styling.

class TTkFrame(TTkContainer):
    def __init__(self, parent=None, **kwargs):
        """
        Initialize a frame widget.
        
        Parameters:
        - border (bool): Show frame border
        - title (str): Frame title
        - titlePos (int): Title position constant
        """
    
    def setBorder(self, border):
        """Enable/disable frame border."""
    
    def border(self):
        """Check if border is enabled."""
    
    def setTitle(self, title):
        """Set the frame title."""
    
    def title(self):
        """Get the frame title."""
    
    def setTitlePos(self, pos):
        """Set title position."""
    
    def titlePos(self):
        """Get title position."""
    
    def setFrameStyle(self, style):
        """Set frame border style."""
    
    def frameStyle(self):
        """Get frame border style."""

Resizable Frame Widget

TTkResizableFrame extends TTkFrame with user-resizable capabilities.

class TTkResizableFrame(TTkFrame):
    def __init__(self, parent=None, **kwargs):
        """
        Initialize a resizable frame widget.
        
        Parameters:
        - resizeBorder (int): Resize border width
        - minSize (TTkSize): Minimum size constraint
        - maxSize (TTkSize): Maximum size constraint
        """
    
    def setResizeBorder(self, border):
        """Set resize border width."""
    
    def resizeBorder(self):
        """Get resize border width."""
    
    def setMinSize(self, size):
        """Set minimum size constraint."""
    
    def minSize(self):
        """Get minimum size constraint."""
    
    def setMaxSize(self, size):
        """Set maximum size constraint."""
    
    def maxSize(self):
        """Get maximum size constraint."""
    
    def isResizing(self):
        """Check if currently being resized."""
    
    # Signals
    resized: pyTTkSignal  # Emitted when frame is resized

Splitter Widget

TTkSplitter provides resizable panes that can be adjusted by the user.

class TTkSplitter(TTkContainer):
    def __init__(self, parent=None, **kwargs):
        """
        Initialize a splitter widget.
        
        Parameters:
        - orientation (int): Horizontal or vertical orientation
        - sizes (list): Initial sizes for panes
        """
    
    def addWidget(self, widget):
        """Add a widget as a new pane."""
    
    def insertWidget(self, index, widget):
        """Insert a widget at specific pane index."""
    
    def setOrientation(self, orientation):
        """Set splitter orientation."""
    
    def orientation(self):
        """Get splitter orientation."""
    
    def setSizes(self, sizes):
        """Set sizes for all panes."""
    
    def sizes(self):
        """Get current sizes of all panes."""
    
    def setCollapsible(self, index, collapsible):
        """Set whether a pane can be collapsed."""
    
    def isCollapsible(self, index):
        """Check if pane can be collapsed."""
    
    def setStretchFactor(self, index, stretch):
        """Set stretch factor for a pane."""
    
    def stretchFactor(self, index):
        """Get stretch factor for a pane."""
    
    def count(self):
        """Get number of panes."""
    
    def widget(self, index):
        """Get widget at pane index."""
    
    def indexOf(self, widget):
        """Get index of widget."""
    
    def setSplitterHandleSize(self, size):
        """Set splitter handle size."""
    
    def splitterHandleSize(self):
        """Get splitter handle size."""
    
    # Signals
    splitterMoved: pyTTkSignal  # Emitted when splitter is moved

Scroll Area Widget

TTkScrollArea provides scrollable content area for widgets larger than the visible area.

class TTkScrollArea(TTkAbstractScrollArea):
    def __init__(self, parent=None, **kwargs):
        """
        Initialize a scroll area widget.
        
        Parameters:
        - widgetResizable (bool): Allow widget to resize with scroll area
        """
    
    def setWidget(self, widget):
        """Set the widget to be scrolled."""
    
    def widget(self):
        """Get the scrolled widget."""
    
    def takeWidget(self):
        """Remove and return the scrolled widget."""
    
    def setWidgetResizable(self, resizable):
        """Set whether widget resizes with scroll area."""
    
    def widgetResizable(self):
        """Check if widget resizes with scroll area."""
    
    def setAlignment(self, alignment):
        """Set widget alignment within scroll area."""
    
    def alignment(self):
        """Get widget alignment."""
    
    def ensureVisible(self, x, y, xmargin=0, ymargin=0):
        """Ensure a point is visible in the scroll area."""
    
    def ensureWidgetVisible(self, childWidget, xmargin=0, ymargin=0):
        """Ensure a child widget is visible."""

Tab Widget

TTkTabWidget provides tabbed container for multiple pages of content.

class TTkTabWidget(TTkContainer):
    def __init__(self, parent=None, **kwargs):
        """
        Initialize a tab widget.
        
        Parameters:
        - closable (bool): Whether tabs have close buttons (default: False)
        - barType (TTkBarType): Tab bar type configuration (default: TTkBarType.NONE)
        """
    
    def addTab(self, widget, label, data=None, closable=None):
        """Add a tab with widget and label."""
    
    def insertTab(self, index, widget, label):
        """Insert a tab at specific index."""
    
    def removeTab(self, index):
        """Remove tab at index."""
    
    def setCurrentIndex(self, index):
        """Set the current active tab."""
    
    def currentIndex(self):
        """Get the current active tab index."""
    
    def setCurrentWidget(self, widget):
        """Set the current active widget."""
    
    def currentWidget(self):
        """Get the current active widget."""
    
    def count(self):
        """Get number of tabs."""
    
    def widget(self, index):
        """Get widget at tab index."""
    
    def indexOf(self, widget):
        """Get index of widget."""
    
    def setTabText(self, index, text):
        """Set text for tab at index."""
    
    def tabText(self, index):
        """Get text for tab at index."""
    
    def setTabEnabled(self, index, enabled):
        """Enable/disable tab at index."""
    
    def isTabEnabled(self, index):
        """Check if tab is enabled."""
    
    def setTabVisible(self, index, visible):
        """Show/hide tab at index."""
    
    def isTabVisible(self, index):
        """Check if tab is visible."""
    
    def setTabPosition(self, position):
        """Set tab bar position."""
    
    def tabPosition(self):
        """Get tab bar position."""
    
    def setTabsClosable(self, closable):
        """Enable/disable tab close buttons."""
    
    def tabsClosable(self):
        """Check if tabs are closable."""
    
    def clear(self):
        """Remove all tabs."""
    
    # Signals
    currentChanged: pyTTkSignal  # Emitted when current tab changes
    tabCloseRequested: pyTTkSignal  # Emitted when tab close is requested
    tabBarClicked: pyTTkSignal  # Emitted when tab bar is clicked

Application Template

TTkAppTemplate provides a common application structure with menu bar, tool bar, status bar, and central widget.

class TTkAppTemplate(TTkWindow):
    def __init__(self, parent=None, **kwargs):
        """
        Initialize an application template.
        
        Parameters:
        - closable (bool): Whether the application can be closed (default: False)
        - barType (TTkBarType): Bar type configuration (default: TTkBarType.NONE)
        """
    
    def setCentralWidget(self, widget):
        """Set the central widget."""
    
    def centralWidget(self):
        """Get the central widget."""
    
    def menuBar(self):
        """Get the menu bar."""
    
    def statusBar(self):
        """Get the status bar."""
    
    def addDockWidget(self, area, widget):
        """Add a docked widget to specified area."""
    
    def removeDockWidget(self, widget):
        """Remove a docked widget."""
    
    def setMenuBar(self, menuBar):
        """Set custom menu bar."""
    
    def setStatusBar(self, statusBar):
        """Set custom status bar."""

Usage Examples

Basic Window Example

import TermTk as ttk

root = ttk.TTk()

# Create a window
window = ttk.TTkWindow(parent=root, 
                      title="My Application",
                      pos=(5, 5), 
                      size=(60, 20))

# Add content to window
label = ttk.TTkLabel(parent=window, 
                    text="Hello from window!", 
                    pos=(10, 5))

button = ttk.TTkButton(parent=window,
                      text="Close",
                      pos=(10, 8),
                      size=(10, 3))

# Connect close button
@ttk.pyTTkSlot()
def close_window():
    window.close()

button.clicked.connect(close_window)

root.mainloop()

Splitter Layout Example

import TermTk as ttk

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

# Create horizontal splitter
splitter = ttk.TTkSplitter(parent=container, 
                          orientation=ttk.TTkConstant.Horizontal)

# Create panes
left_frame = ttk.TTkFrame(title="Left Pane")
left_label = ttk.TTkLabel(parent=left_frame, text="Left content")

right_frame = ttk.TTkFrame(title="Right Pane")  
right_label = ttk.TTkLabel(parent=right_frame, text="Right content")

# Add panes to splitter
splitter.addWidget(left_frame)
splitter.addWidget(right_frame)

# Set initial sizes (left: 30%, right: 70%)
splitter.setSizes([30, 70])

root.mainloop()

Scrollable Content Example

import TermTk as ttk

root = ttk.TTk()
container = ttk.TTkContainer(parent=root, size=(50, 20))

# Create scroll area
scroll = ttk.TTkScrollArea(parent=container)
scroll.setWidgetResizable(True)

# Create large content widget
content = ttk.TTkContainer()
layout = ttk.TTkVBoxLayout()

# Add many widgets to demonstrate scrolling
for i in range(50):
    button = ttk.TTkButton(text=f"Button {i+1}")
    layout.addWidget(button)

content.setLayout(layout)
scroll.setWidget(content)

root.mainloop()

Tabbed Interface Example

import TermTk as ttk

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

# Create tab widget
tabs = ttk.TTkTabWidget(parent=container)

# Create tab pages
page1 = ttk.TTkContainer()
page1_layout = ttk.TTkVBoxLayout()
page1_layout.addWidget(ttk.TTkLabel(text="This is page 1"))
page1_layout.addWidget(ttk.TTkButton(text="Page 1 Button"))
page1.setLayout(page1_layout)

page2 = ttk.TTkContainer()
page2_layout = ttk.TTkVBoxLayout()
page2_layout.addWidget(ttk.TTkLabel(text="This is page 2"))
page2_layout.addWidget(ttk.TTkLineEdit(text="Edit field"))
page2.setLayout(page2_layout)

page3 = ttk.TTkContainer()
page3_layout = ttk.TTkVBoxLayout()
page3_layout.addWidget(ttk.TTkLabel(text="This is page 3"))
page3_layout.addWidget(ttk.TTkCheckbox(text="Option"))
page3.setLayout(page3_layout)

# Add tabs
tabs.addTab(page1, "General")
tabs.addTab(page2, "Settings")
tabs.addTab(page3, "Options")

# Handle tab changes
@ttk.pyTTkSlot(int)
def tab_changed(index):
    print(f"Switched to tab: {tabs.tabText(index)}")

tabs.currentChanged.connect(tab_changed)

root.mainloop()

Application Template Example

import TermTk as ttk

root = ttk.TTk()

# Create application template
app = ttk.TTkAppTemplate(parent=root, title="My Application")

# Set up menu bar
menu_bar = app.menuBar()
file_menu = menu_bar.addMenu("File")
file_menu.addAction("New", lambda: print("New"))
file_menu.addAction("Open", lambda: print("Open"))
file_menu.addSeparator()
file_menu.addAction("Exit", app.close)

edit_menu = menu_bar.addMenu("Edit")
edit_menu.addAction("Cut", lambda: print("Cut"))
edit_menu.addAction("Copy", lambda: print("Copy"))
edit_menu.addAction("Paste", lambda: print("Paste"))

# Set up central widget
central = ttk.TTkContainer()
layout = ttk.TTkVBoxLayout()
layout.addWidget(ttk.TTkLabel(text="Main Content Area"))
layout.addWidget(ttk.TTkTextEdit())
central.setLayout(layout)
app.setCentralWidget(central)

# Set up status bar
status_bar = app.statusBar()
status_bar.showMessage("Ready")

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