简体中文 | English | More translations are welcome!
Themed Tkinter Text widget with modern styling support.
- 🎨 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
pip install ttk-text
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()
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")
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())
Example screenshots of Windows 11, Windows 10, and Windows 7.
See CONTRIBUTING.md for details.
This project is licensed under the MIT License, see the LICENSE file for details.