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

utilities.mddocs/

Utilities and System Classes

Utility classes, system services, and helper functionality for pyTermTk applications including timers, clipboard access, keyboard shortcuts, and application helpers.

Capabilities

Helper Utilities

TTkHelper provides static utility methods for common application tasks and system operations.

class TTkHelper:
    @staticmethod
    def quit():
        """Quit the current application."""
    
    @staticmethod
    def aboutTermTk():
        """Show information about pyTermTk library."""
    
    @staticmethod
    def getTerminalSize():
        """Get current terminal size as (width, height)."""
    
    @staticmethod
    def updateAll():
        """Force update of all widgets."""
    
    @staticmethod
    def overlay(widget):
        """Add widget as overlay on top of all other widgets."""
    
    @staticmethod
    def removeOverlayAndChild(widget):
        """Remove widget from overlay and destroy."""
    
    # Signals
    quitEvent: pyTTkSignal  # Emitted when application quit is requested

Timer System

TTkTimer provides timed events and periodic callbacks for animations and scheduled tasks.

class TTkTimer:
    def __init__(self):
        """Initialize a timer object."""
    
    def start(self, sec=0.0):
        """
        Start the timer.
        
        Parameters:
        - sec (float): Timeout in seconds (0.0 for immediate single-shot)
        """
    
    def stop(self):
        """Stop the timer."""
    
    def quit(self):
        """Stop and cleanup the timer."""
    
    def isActive(self):
        """Check if timer is currently active."""
    
    def setSingleShot(self, singleShot):
        """Set whether timer fires once or repeatedly."""
    
    def isSingleShot(self):
        """Check if timer is single-shot."""
    
    def setInterval(self, sec):
        """Set timer interval in seconds."""
    
    def interval(self):
        """Get timer interval."""
    
    # Signals
    timeout: pyTTkSignal  # Emitted when timer expires

Clipboard Access

TTkClipboard provides system clipboard integration for copy/paste operations.

class TTkClipboard:
    @staticmethod
    def setText(text):
        """
        Set text content to system clipboard.
        
        Parameters:
        - text (str): Text to copy to clipboard
        """
    
    @staticmethod
    def text():
        """
        Get text content from system clipboard.
        
        Returns:
        - str: Clipboard text content
        """
    
    @staticmethod
    def clear():
        """Clear clipboard content."""
    
    @staticmethod
    def hasText():
        """Check if clipboard contains text."""

Spacer Widget

TTkSpacer provides invisible spacing for layouts.

class TTkSpacer(TTkWidget):
    def __init__(self, parent=None, **kwargs):
        """
        Initialize a spacer widget.
        
        Parameters:
        - sizePolicy: Size policy for expansion behavior
        """
    
    def setSizePolicy(self, policy):
        """Set the size policy for space expansion."""
    
    def sizePolicy(self):
        """Get the size policy."""

Menu System

TTkMenuBarLayout and TTkMenu provide menu bar and dropdown menu functionality.

class TTkMenuBarLayout(TTkLayout):
    def __init__(self, **kwargs):
        """Initialize a menu bar layout."""
    
    def addMenu(self, text, data=None, checkable=False, checked=False, alignment=TTkK.LEFT_ALIGN):
        """
        Add a menu to the menu bar.
        
        Parameters:
        - text (str): Menu text
        - data: Associated data
        - checkable (bool): Whether menu can be checked
        - checked (bool): Initial checked state
        - alignment: Menu alignment
        
        Returns:
        - TTkMenuButton: The created menu button
        """
    
    def clear(self):
        """Clear all menus."""

class TTkMenu(TTkContainer):
    def __init__(self, parent=None, **kwargs):
        """Initialize a menu widget."""
    
    def addAction(self, text, callback=None, data=None, checkable=False, checked=False):
        """Add an action to the menu."""
    
    def addSeparator(self):
        """Add a separator line to the menu."""
    
    def addSubMenu(self, text, submenu):
        """Add a submenu."""
    
    def clear(self):
        """Clear all menu items."""
    
    def exec(self, pos=None):
        """Execute the menu at specified position."""
    
    # Signals
    triggered: pyTTkSignal    # Emitted when menu item is triggered
    aboutToShow: pyTTkSignal  # Emitted before menu is shown
    aboutToHide: pyTTkSignal  # Emitted before menu is hidden

class TTkMenuButton(TTkButton):
    def __init__(self, parent=None, **kwargs):
        """Initialize a menu button."""
    
    def setMenu(self, menu):
        """Set the dropdown menu."""
    
    def menu(self):
        """Get the dropdown menu."""

Usage Examples

Timer Example

import TermTk as ttk

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

# Create a label to show time
time_label = ttk.TTkLabel(parent=container, text="Timer: 0", pos=(5, 5))

# Create timer
timer = ttk.TTkTimer()
counter = 0

@ttk.pyTTkSlot()
def update_timer():
    global counter
    counter += 1
    time_label.setText(f"Timer: {counter}")

timer.timeout.connect(update_timer)
timer.start(1.0)  # Update every second

root.mainloop()

Clipboard Example

import TermTk as ttk

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

# Text edit for clipboard operations
text_edit = ttk.TTkTextEdit()
text_edit.setText("Sample text for clipboard operations")

# Buttons for clipboard operations
copy_btn = ttk.TTkButton(text="Copy to Clipboard")
paste_btn = ttk.TTkButton(text="Paste from Clipboard")

@ttk.pyTTkSlot()
def copy_text():
    selected = text_edit.selectedText()
    if selected:
        ttk.TTkClipboard.setText(selected)
    else:
        ttk.TTkClipboard.setText(text_edit.text())

@ttk.pyTTkSlot()
def paste_text():
    clipboard_text = ttk.TTkClipboard.text()
    if clipboard_text:
        text_edit.insertText(clipboard_text)

copy_btn.clicked.connect(copy_text)
paste_btn.clicked.connect(paste_text)

# Layout
button_layout = ttk.TTkHBoxLayout()
button_layout.addWidget(copy_btn)
button_layout.addWidget(paste_btn)

layout.addWidget(text_edit)
layout.addLayout(button_layout)

container.setLayout(layout)
root.mainloop()

Menu System Example

import TermTk as ttk

root = ttk.TTk()
app = ttk.TTkAppTemplate(parent=root, title="Menu Example")

# Get menu bar from app template
menu_bar = app.menuBar()

# File menu
file_menu = menu_bar.addMenu("File")
file_menu.addAction("New", lambda: print("New file"))
file_menu.addAction("Open", lambda: print("Open file"))
file_menu.addSeparator()
file_menu.addAction("Save", lambda: print("Save file"))
file_menu.addAction("Save As", lambda: print("Save As"))
file_menu.addSeparator()
file_menu.addAction("Exit", ttk.TTkHelper.quit)

# Edit menu
edit_menu = menu_bar.addMenu("Edit")
edit_menu.addAction("Undo", lambda: print("Undo"))
edit_menu.addAction("Redo", lambda: print("Redo"))
edit_menu.addSeparator()
edit_menu.addAction("Cut", lambda: print("Cut"))
edit_menu.addAction("Copy", lambda: print("Copy"))
edit_menu.addAction("Paste", lambda: print("Paste"))

# View menu with checkable items
view_menu = menu_bar.addMenu("View")
view_menu.addAction("Show Toolbar", checkable=True, checked=True)
view_menu.addAction("Show Status Bar", checkable=True, checked=True)
view_menu.addSeparator()
view_menu.addAction("Full Screen", lambda: print("Toggle full screen"))

# Help menu
help_menu = menu_bar.addMenu("Help")
help_menu.addAction("About", ttk.TTkHelper.aboutTermTk)
help_menu.addAction("Documentation", lambda: print("Show docs"))

# Set central widget
central = ttk.TTkContainer()
layout = ttk.TTkVBoxLayout()
layout.addWidget(ttk.TTkLabel(text="Application with menu system"))
central.setLayout(layout)
app.setCentralWidget(central)

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