The Text widget in Tkinter is a versatile widget used for displaying and editing multi-line text. It supports text formatting, styling, and advanced features like tagging and marking text for more complex text manipulation.
w = Text(parent, options)
• parent: The parent widget or window in which the Text widget will be placed.
• options: Various configuration options for the Text widget (listed below).
| SN | Option | Description |
|---|---|---|
| 1 | bg | Background color of the widget. |
| 2 | bd | Border width of the widget. |
| 3 | cursor | Mouse pointer type when hovering over the widget (e.g., arrow, dot). |
| 4 | exportselection | Export the selected text to the system clipboard. Set to 0 to disable. |
| 5 | font | Font type used for the text. |
| 6 | fg | Foreground color of the text. |
| 7 | height | Vertical dimension of the widget in lines. |
| 8 | highlightbackground | Highlight color when the widget does not have focus. |
| 9 | highlightthickness | Thickness of the focus highlight (default is 1). |
| 10 | highlightcolor | Highlight color when the widget has focus. |
| 11 | insertbackground | Color of the insertion cursor (caret). |
| 12 | insertborderwidth | Width of the border around the insertion cursor (default is 0). |
| 13 | insertofftime | Time in milliseconds that the cursor is off in the blink cycle. |
| 14 | insertontime | Time in milliseconds that the cursor is on in the blink cycle. |
| 15 | insertwidth | Width of the insertion cursor. |
| 16 | padx | Horizontal padding of the widget. |
| 17 | pady | Vertical padding of the widget. |
| 18 | relief | Type of border (default is SUNKEN). |
| 19 | selectbackground | Background color of the selected text. |
| 20 | selectborderwidth | Width of the border around the selected text. |
| 21 | spacing1 | Vertical space above each line of text. Default is 0. |
| 22 | spacing2 | Extra vertical space between lines of text when a logical line wraps. Default is 0. |
| 23 | spacing3 | Vertical space below each line of text. |
| 24 | state | State of the widget (NORMAL or DISABLED). |
| 25 | tabs | Controls tabulation. |
| 26 | width | Width of the widget in characters. |
| 27 | wrap | Text wrapping mode (CHAR for character-based, WORD for word-based). |
| 28 | xscrollcommand | For horizontal scrolling, connect to the set() method of a horizontal Scrollbar. |
| 29 | yscrollcommand | For vertical scrolling, connect to the set() method of a vertical Scrollbar. |
| SN | Method | Description |
|---|---|---|
| 1 | delete(startindex, endindex) | Deletes characters in the specified range from startindex to endindex. |
| 2 | get(startindex, endindex) | Returns the text in the specified range from startindex to endindex. |
| 3 | index(index) | Returns the absolute index for the specified index. |
| 4 | insert(index, string) | Inserts the specified string at the given index. |
| 5 | see(index) | Scrolls the text widget to ensure that the text at the given index is visible. |
Marks are used to bookmark specific positions in the text.
| SN | Method | Description |
|---|---|---|
| 1 | index(mark) | Returns the index of the specified mark. |
| 2 | mark_gravity(mark, gravity) | Gets the gravity of the given mark. |
| 3 | mark_names() | Returns a list of all marks present in the Text widget. |
| 4 | mark_set(mark, index) | Sets the position of the given mark to the specified index. |
| 5 | mark_unset(mark) | Removes the specified mark from the text. |
| SN | Method | Description |
|---|---|---|
| 1 | tag_add(tagname, startindex, endindex) | Tags the text in the specified range with tagname. |
| 2 | tag_config(tagname, options) | Configures properties for the specified tag. |
| 3 | tag_delete(tagname) | Deletes the specified tag. |
| 4 | tag_remove(tagname, startindex, endindex) | Removes the specified tag from the text in the given range. |
Here's a basic example of how to use the Text widget in Tkinter:
from tkinter import *
def main():
root = Tk()
root.title("Text Widget Example")
# Create a Text widget
text_widget = Text(root, wrap=WORD, height=10, width=40, padx=10, pady=10)
text_widget.pack()
# Insert some text
text_widget.insert(INSERT, "This is an example of using the Text widget.\n")
text_widget.insert(INSERT, "You can add multiple lines of text and format them as needed.")
# Add a tag
text_widget.tag_add("bold", "1.0", "1.end")
text_widget.tag_config("bold", font=("Helvetica", 12, "bold"))
# Run the Tkinter event loop
root.mainloop()
if __name__ == "__main__":
main()
• wrap=WORD: The text will wrap at word boundaries.
• eight and width: Specifies the size of the Text widget in lines and characters.
• padx and pady: Adds padding inside the widget.
• tag_add and tag_config: Tags a portion of text and configures its style.
This example demonstrates how to create a Text widget, insert text, and apply a simple tag for formatting.