Python Terminal Toolkit for creating sophisticated text-based user interfaces with Qt-like API and full widget support
—
Widgets for displaying information including labels, images, progress bars, and other visual components.
TTkLabel displays text with support for formatting, alignment, and color styling.
class TTkLabel(TTkWidget):
def __init__(self, parent=None, text="", **kwargs):
"""
Initialize a label widget.
Parameters:
- text (str): Text content to display
- color (TTkColor): Text color
- alignment (int): Text alignment constant
- wordWrap (bool): Enable word wrapping
"""
def setText(self, text):
"""Set the label text."""
def text(self):
"""Get the label text."""
def setAlignment(self, alignment):
"""Set text alignment."""
def alignment(self):
"""Get text alignment."""
def setWordWrap(self, wrap):
"""Enable/disable word wrapping."""
def wordWrap(self):
"""Check if word wrapping is enabled."""
def setColor(self, color):
"""Set text color."""
def color(self):
"""Get text color."""
def setPixmap(self, pixmap):
"""Set a pixmap/image to display."""
def pixmap(self):
"""Get the current pixmap."""
def clear(self):
"""Clear the label content."""TTkImage displays images and graphics in the terminal using text characters and colors.
class TTkImage(TTkWidget):
def __init__(self, parent=None, **kwargs):
"""
Initialize an image widget.
Parameters:
- image: Image data to display
- scaledContents (bool): Scale image to fit widget
- alignment (int): Image alignment
"""
def setImage(self, image):
"""Set the image to display."""
def image(self):
"""Get the current image."""
def setPixmap(self, pixmap):
"""Set a pixmap to display."""
def pixmap(self):
"""Get the current pixmap."""
def setScaledContents(self, scaled):
"""Enable/disable content scaling."""
def hasScaledContents(self):
"""Check if content scaling is enabled."""
def setAlignment(self, alignment):
"""Set image alignment."""
def alignment(self):
"""Get image alignment."""
def clear(self):
"""Clear the image."""
def loadFromFile(self, filename):
"""Load image from file."""TTkGraph provides plotting capabilities for data visualization in terminal.
class TTkGraph(TTkWidget):
def __init__(self, parent=None, **kwargs):
"""
Initialize a graph widget.
Parameters:
- title (str): Graph title
- xLabel (str): X-axis label
- yLabel (str): Y-axis label
"""
def addData(self, data, label=None, color=None):
"""
Add data series to the graph.
Parameters:
- data: List of (x, y) tuples or y values
- label (str): Data series label
- color (TTkColor): Line/point color
"""
def clearData(self):
"""Clear all data series."""
def setTitle(self, title):
"""Set graph title."""
def title(self):
"""Get graph title."""
def setXLabel(self, label):
"""Set X-axis label."""
def xLabel(self):
"""Get X-axis label."""
def setYLabel(self, label):
"""Set Y-axis label."""
def yLabel(self):
"""Get Y-axis label."""
def setXRange(self, min_val, max_val):
"""Set X-axis range."""
def setYRange(self, min_val, max_val):
"""Set Y-axis range."""
def setAutoRange(self, enabled):
"""Enable/disable automatic range adjustment."""
def setGridEnabled(self, enabled):
"""Enable/disable grid display."""
def setLegendEnabled(self, enabled):
"""Enable/disable legend display."""
def refresh(self):
"""Refresh the graph display."""TTkFancyProgressBar displays progress indication with customizable styling.
class TTkFancyProgressBar(TTkWidget):
def __init__(self, parent=None, **kwargs):
"""
Initialize a progress bar widget.
Parameters:
- value (float): Initial value (default: 0.0)
- lookAndFeel (TTkLookAndFeelFPBar): Custom styling (optional)
"""
def setValue(self, value):
"""Set the current progress value."""
def value(self):
"""Get the current progress value."""
def setMinimum(self, minimum):
"""Set the minimum value."""
def minimum(self):
"""Get the minimum value."""
def setMaximum(self, maximum):
"""Set the maximum value."""
def maximum(self):
"""Get the maximum value."""
def setRange(self, minimum, maximum):
"""Set the value range."""
def reset(self):
"""Reset progress to minimum value."""
def setOrientation(self, orientation):
"""Set progress bar orientation."""
def orientation(self):
"""Get progress bar orientation."""
def setTextVisible(self, visible):
"""Show/hide progress text."""
def isTextVisible(self):
"""Check if progress text is visible."""
def setFormat(self, format_str):
"""Set progress text format string."""
def format(self):
"""Get progress text format string."""
def setInvertedAppearance(self, inverted):
"""Invert the progress bar appearance."""
def invertedAppearance(self):
"""Check if appearance is inverted."""
# Signals
valueChanged: pyTTkSignal # Emitted when value changesTTkAbout displays application information in a dialog format.
class TTkAbout(TTkWidget):
def __init__(self, parent=None, **kwargs):
"""
Initialize an about dialog widget.
Parameters:
- title (str): Dialog title
- text (str): About text content
- logo: Logo image to display
"""
def setTitle(self, title):
"""Set the dialog title."""
def title(self):
"""Get the dialog title."""
def setText(self, text):
"""Set the about text content."""
def text(self):
"""Get the about text content."""
def setLogo(self, logo):
"""Set the logo image."""
def logo(self):
"""Get the logo image."""
def setVersion(self, version):
"""Set the application version."""
def version(self):
"""Get the application version."""
def setCopyright(self, copyright_text):
"""Set the copyright text."""
def copyright(self):
"""Get the copyright text."""
def addAuthor(self, name, email=None, role=None):
"""Add an author to the about information."""
def addCredit(self, name, task):
"""Add a credit entry."""
def addLicense(self, name, text):
"""Add license information."""import TermTk as ttk
root = ttk.TTk()
container = ttk.TTkContainer(parent=root)
layout = ttk.TTkVBoxLayout()
# Simple text label
title = ttk.TTkLabel(text="Application Title")
title.setAlignment(ttk.TTkConstant.AlignCenter)
# Colored label
status = ttk.TTkLabel(text="Status: Ready")
green_color = ttk.TTkColor(fg="#00FF00")
status.setColor(green_color)
# Multi-line label with word wrap
description = ttk.TTkLabel(text="This is a long description that will wrap to multiple lines when the widget is not wide enough to display it all on one line.")
description.setWordWrap(True)
layout.addWidget(title)
layout.addWidget(status)
layout.addWidget(description)
container.setLayout(layout)
root.mainloop()import TermTk as ttk
import time
root = ttk.TTk()
container = ttk.TTkContainer(parent=root)
layout = ttk.TTkVBoxLayout()
# Create progress bar
progress = ttk.TTkFancyProgressBar(minimum=0, maximum=100, value=0)
progress.setTextVisible(True)
progress.setFormat("Progress: %p%")
# Label to show current value
label = ttk.TTkLabel(text="Starting...")
layout.addWidget(label)
layout.addWidget(progress)
container.setLayout(layout)
# Simulate progress updates
def update_progress():
for i in range(101):
progress.setValue(i)
label.setText(f"Processing item {i}/100")
root.update() # Force UI update
time.sleep(0.1) # Simulate work
label.setText("Complete!")
# Start progress in a timer (in real app, use threading)
timer = ttk.TTkTimer()
timer.timeout.connect(update_progress)
timer.start(100) # Start after 100ms
root.mainloop()import TermTk as ttk
import math
root = ttk.TTk()
container = ttk.TTkContainer(parent=root)
# Create graph widget
graph = ttk.TTkGraph(parent=container,
title="Sine and Cosine Waves",
xLabel="X Values",
yLabel="Y Values")
# Generate data
x_data = [i * 0.1 for i in range(100)]
sin_data = [(x, math.sin(x)) for x in x_data]
cos_data = [(x, math.cos(x)) for x in x_data]
# Add data series
red_color = ttk.TTkColor(fg="#FF0000")
blue_color = ttk.TTkColor(fg="#0000FF")
graph.addData(sin_data, label="sin(x)", color=red_color)
graph.addData(cos_data, label="cos(x)", color=blue_color)
# Configure graph
graph.setGridEnabled(True)
graph.setLegendEnabled(True)
graph.setAutoRange(True)
root.mainloop()import TermTk as ttk
root = ttk.TTk()
container = ttk.TTkContainer(parent=root)
layout = ttk.TTkVBoxLayout()
# Create image widget
image = ttk.TTkImage()
image.loadFromFile("logo.png") # Load from file
image.setScaledContents(True) # Scale to fit widget
image.setAlignment(ttk.TTkConstant.AlignCenter)
# Image caption
caption = ttk.TTkLabel(text="Company Logo")
caption.setAlignment(ttk.TTkConstant.AlignCenter)
layout.addWidget(image)
layout.addWidget(caption)
container.setLayout(layout)
root.mainloop()import TermTk as ttk
root = ttk.TTk()
# Create about dialog
about = ttk.TTkAbout(parent=root,
title="About My App",
text="My Application v1.0\n\nA terminal-based application built with pyTermTk.")
about.setVersion("1.0.0")
about.setCopyright("Copyright (c) 2024 My Company")
about.addAuthor("John Doe", "john@example.com", "Lead Developer")
about.addAuthor("Jane Smith", "jane@example.com", "UI Designer")
about.addCredit("Bob Johnson", "Testing and Quality Assurance")
about.addLicense("MIT", "Permission is hereby granted, free of charge...")
about.show()
root.mainloop()TTkScrollBar provides scrolling controls for navigating content that exceeds the visible area.
class TTkScrollBar(TTkWidget):
def __init__(self, parent=None, **kwargs):
"""
Initialize a scroll bar widget.
Parameters:
- value (int): Initial value (default: 0)
- minimum (int): Minimum value (default: 0)
- maximum (int): Maximum value (default: 99)
- singleStep (int): Single step size (default: 1)
- pageStep (int): Page step size (default: 10)
- orientation (TTkK.Direction): Orientation (default: TTkK.VERTICAL)
"""
def setValue(self, value):
"""Set the current value."""
def value(self):
"""Get the current value."""
def setMinimum(self, minimum):
"""Set the minimum value."""
def minimum(self):
"""Get the minimum value."""
def setMaximum(self, maximum):
"""Set the maximum value."""
def maximum(self):
"""Get the maximum value."""
def setRange(self, minimum, maximum):
"""Set the value range."""
def setSingleStep(self, step):
"""Set single step size."""
def singleStep(self):
"""Get single step size."""
def setPageStep(self, step):
"""Set page step size."""
def pageStep(self):
"""Get page step size."""
def setOrientation(self, orientation):
"""Set scroll bar orientation."""
def orientation(self):
"""Get scroll bar orientation."""
def setInvertedControls(self, inverted):
"""Invert scroll direction."""
def invertedControls(self):
"""Check if controls are inverted."""
# Signals
valueChanged: pyTTkSignal # Emitted when value changes
rangeChanged: pyTTkSignal # Emitted when range changes
sliderMoved: pyTTkSignal # Emitted when slider is movedInstall with Tessl CLI
npx tessl i tessl/pypi-pytermtk