Skip to content

HelloTool/TtkText

TtkText

简体中文 | English | More translations are welcome!

Contributor Covenant MIT License Testing PyPI - Downloads PyPI - Version

Themed Tkinter Text widget with modern styling support.

Features

  • 🎨 Theme-aware text widget that automatically adapts to ttk themes
  • 📜 Built-in ScrolledText component with vertical/horizontal scrollbars
  • 🖥️ Native integration with ttk styles and themes
  • 🔄 Dynamic theme switching support

Installation

pip install ttk-text

Quick Start

from tkinter import Tk
from ttk_text import ThemedText
from ttk_text.scrolled_text import ScrolledText

root = Tk()
themed_text = ThemedText(root)
themed_text.pack(fill="both", expand=True)

scrolled_text = ScrolledText(root)
scrolled_text.pack(fill="both", expand=True)

root.mainloop()

Usage

Styling Configuration

The ThemedText component works by wrapping a Text widget inside a ttk Frame. This Frame uses the default style name ThemedText.TEntry, which you can use to customize its appearance.

Property Description
borderwidth Frame border width
padding Frame padding
fieldbackground Text background color
foreground Text font color
textpadding Text external padding
insertwidth Text cursor width
selectbackground Text selection background color
selectforeground Text selection font color

Example: Set border width to 1.5p.

from tkinter.ttk import Style

style = Style()
style.configure("ThemedText.TEntry", borderwidth="1.5p")

Theme Fixes

Some third-party themes may not be fully compatible with TtkText. You can call the following function after setting the theme:

Sun Valley ttk theme
from tkinter.ttk import Style
import sv_ttk


def fix_sv_ttk(style: Style):
    if sv_ttk.get_theme() == "light":
        style.configure("ThemedText.TEntry", fieldbackground="#fdfdfd", textpadding=5)
        style.map(
            "ThemedText.TEntry",
            fieldbackground=[
                ("hover", "!focus", "#f9f9f9"),
            ],
            foreground=[
                ("pressed", style.lookup("TEntry", "foreground")),
            ]
        )
    else:
        style.configure("ThemedText.TEntry", fieldbackground="#292929", textpadding=5)
        style.map(
            "ThemedText.TEntry",
            fieldbackground=[
                ("hover", "!focus", "#2f2f2f"),
                ("focus", "#1c1c1c"),
            ],
            foreground=[
                ("pressed", style.lookup("TEntry", "foreground")),
            ]
        )

sv_ttk.set_theme("light")
fix_sv_ttk(Style())

Screenshots

Windows 11 Windows 10 Windows 7

Example screenshots of Windows 11, Windows 10, and Windows 7.

Contributing

See CONTRIBUTING.md for details.

License

This project is licensed under the MIT License, see the LICENSE file for details.

About

Themed Tkinter Text widget with modern styling support.

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages