Python Terminal Toolkit for creating sophisticated text-based user interfaces with Qt-like API and full widget support
—
The foundational classes and widgets that form the basis of pyTermTk applications, including the main application class, base widget functionality, and essential UI components.
The TTk class serves as the main application container and event loop manager for pyTermTk applications.
class TTk:
def __init__(self, **kwargs):
"""
Initialize the main application.
Parameters:
- mouseTrack (bool): Enable mouse tracking (default: True)
- sigmask (bool): Enable signal masking (default: True)
"""
def mainloop(self):
"""Start the main event loop."""
def quit(self):
"""Quit the application and exit the main loop."""
def addWidget(self, widget):
"""Add a widget to the root container."""
def removeWidget(self, widget):
"""Remove a widget from the root container."""
def setMaxSize(self, size):
"""Set the maximum size of the terminal."""
def getCanvas(self):
"""Get the main canvas for drawing operations."""TTkWidget is the base class for all UI components, providing fundamental functionality for positioning, sizing, event handling, and drawing.
class TTkWidget:
def __init__(self, parent=None, **kwargs):
"""
Initialize a widget.
Parameters:
- parent (TTkWidget): Parent widget
- pos (TTkPoint): Position as (x, y) tuple
- size (TTkSize): Size as (width, height) tuple
- name (str): Widget name for identification
- visible (bool): Initial visibility state (default: True)
- enabled (bool): Initial enabled state (default: True)
- focusPolicy (int): Focus policy constant
- toolTip (str): Tooltip text
"""
def setParent(self, parent):
"""Set the parent widget."""
def parent(self):
"""Get the parent widget."""
def children(self):
"""Get list of child widgets."""
def show(self):
"""Show the widget."""
def hide(self):
"""Hide the widget."""
def isVisible(self):
"""Check if widget is visible."""
def setVisible(self, visible):
"""Set widget visibility."""
def setEnabled(self, enabled):
"""Set widget enabled state."""
def isEnabled(self):
"""Check if widget is enabled."""
def setFocus(self):
"""Set keyboard focus to this widget."""
def hasFocus(self):
"""Check if widget has keyboard focus."""
def setFocusPolicy(self, policy):
"""Set the focus policy for the widget."""
def pos(self):
"""Get widget position as (x, y) tuple."""
def setPos(self, x, y):
"""Set widget position."""
def size(self):
"""Get widget size as (width, height) tuple."""
def setSize(self, width, height):
"""Set widget size."""
def geometry(self):
"""Get widget geometry as (x, y, width, height)."""
def setGeometry(self, x, y, width, height):
"""Set widget geometry."""
def move(self, x, y):
"""Move widget to new position."""
def resize(self, width, height):
"""Resize widget to new dimensions."""
def update(self):
"""Schedule widget for repainting."""
def setName(self, name):
"""Set widget name."""
def name(self):
"""Get widget name."""
def setToolTip(self, toolTip):
"""Set tooltip text."""
def toolTip(self):
"""Get tooltip text."""
def setMaximumSize(self, width, height):
"""Set maximum size constraints."""
def setMinimumSize(self, width, height):
"""Set minimum size constraints."""
def maximumSize(self):
"""Get maximum size constraints."""
def minimumSize(self):
"""Get minimum size constraints."""
def getCanvas(self):
"""Get the widget's drawing canvas."""
def paintEvent(self, canvas):
"""Override to implement custom painting."""
def keyEvent(self, evt):
"""Override to handle key events."""
def mousePressEvent(self, evt):
"""Override to handle mouse press events."""
def mouseReleaseEvent(self, evt):
"""Override to handle mouse release events."""
def mouseMoveEvent(self, evt):
"""Override to handle mouse move events."""
def wheelEvent(self, evt):
"""Override to handle mouse wheel events."""
def resizeEvent(self, width, height):
"""Override to handle resize events."""
def focusInEvent(self):
"""Override to handle focus in events."""
def focusOutEvent(self):
"""Override to handle focus out events."""TTkContainer extends TTkWidget to provide child widget management and layout capabilities.
class TTkContainer(TTkWidget):
def __init__(self, parent=None, **kwargs):
"""
Initialize a container widget.
Parameters:
- layout (TTkLayout): Layout manager for child widgets
- padding (TTkPadding): Padding around content area
"""
def addWidget(self, widget):
"""Add a child widget to the container."""
def removeWidget(self, widget):
"""Remove a child widget from the container."""
def setLayout(self, layout):
"""Set the layout manager for this container."""
def layout(self):
"""Get the current layout manager."""
def setPadding(self, padding):
"""Set padding around the content area."""
def getPadding(self):
"""Get the current padding configuration."""TTkPadding defines padding/margins around container content.
class TTkPadding:
def __init__(self, top=0, bottom=0, left=0, right=0):
"""
Initialize padding configuration.
Parameters:
- top (int): Top padding in characters
- bottom (int): Bottom padding in characters
- left (int): Left padding in characters
- right (int): Right padding in characters
"""
def setTop(self, top):
"""Set top padding."""
def setBottom(self, bottom):
"""Set bottom padding."""
def setLeft(self, left):
"""Set left padding."""
def setRight(self, right):
"""Set right padding."""
def top(self):
"""Get top padding."""
def bottom(self):
"""Get bottom padding."""
def left(self):
"""Get left padding."""
def right(self):
"""Get right padding."""Helper classes and utilities for widget management.
class TTkSpacer(TTkWidget):
def __init__(self, parent=None, **kwargs):
"""
A spacer widget for layout spacing.
Parameters:
- sizePolicy: Size policy for expanding behavior
"""
def setSizePolicy(self, policy):
"""Set the size policy for space expansion."""
def TTkHelper:
@staticmethod
def alignCenter(size, area):
"""Calculate centered position within an area."""
@staticmethod
def alignRight(size, area):
"""Calculate right-aligned position within an area."""
@staticmethod
def alignBottom(size, area):
"""Calculate bottom-aligned position within an area."""import TermTk as ttk
# Create main application
root = ttk.TTk()
# Create a basic widget
widget = ttk.TTkWidget(parent=root, pos=(10, 5), size=(20, 10))
widget.setName("MyWidget")
widget.setToolTip("This is a basic widget")
# Create a container with padding
container = ttk.TTkContainer(parent=root, pos=(5, 5), size=(30, 15))
padding = ttk.TTkPadding(top=1, bottom=1, left=2, right=2)
container.setPadding(padding)
# Add child widgets to container
child1 = ttk.TTkWidget(parent=container)
child2 = ttk.TTkWidget(parent=container)
# Start the application
root.mainloop()import TermTk as ttk
class CustomWidget(ttk.TTkWidget):
def __init__(self, parent=None, **kwargs):
super().__init__(parent=parent, **kwargs)
self.clicked_count = 0
def mousePressEvent(self, evt):
self.clicked_count += 1
print(f"Widget clicked {self.clicked_count} times")
self.update() # Trigger repaint
def paintEvent(self, canvas):
# Custom drawing
canvas.drawText((1, 1), f"Clicks: {self.clicked_count}")
# Usage
root = ttk.TTk()
custom = CustomWidget(parent=root, pos=(5, 5), size=(20, 10))
root.mainloop()Install with Tessl CLI
npx tessl i tessl/pypi-pytermtk