3. tk Text


3.1. Usage

The tkinter.Text widget provides a versatile multi-line text area that you can use for various purposes such as comments or descriptions
To create a multi-line text widget the general syntax is (assuming import via "import tkinter as tk"):
text_widget  = tk.Text(parent, option=value)
parent is the window or frame object.
Options can be passed as parameters separated by commas.
Options can also be added after widget creation.
text_widget.config(option=value)
text_widget is the Text widget object.
Options can be passed as parameters separated by commas.

3.2. Text widget example

../_images/text.png
The code below sets some widget options at creation.
It also uses .config() to add other custom options.
import tkinter as tk

root = tk.Tk()
root.title("Text Widget Example")

# Create a Text widget
text = tk.Text(root, height=6, width=40, wrap="word", font=("Helvetica", 12))
text.pack(padx=10, pady=10)

# Insert initial content
text.insert(
    "1.0", "Welcome to \nthe Text Widget!\nIt has multiline text.")

# Customize options
text.config(
    bg="#fafafa",  # Background color
    fg="blue",  # Text color
    bd=1,  # Border width
    relief="solid",  # Border style
    insertbackground="blue",  # Insertion cursor color
    state="normal",  # Enable editing (use "disabled" to disable)
    highlightthickness=1,
    highlightcolor="blue",
    padx=10,
    pady=10,
    yscrollcommand="True",
)

root.mainloop()

Tasks

  1. Modify the given Tkinter code to change the following options to use a background color of light yellow, a text color of dark green, a border width of 2, a border style of "groove" and disabled state so it can't be edited.

    ../_images/text_question.png

Modify the given Tkinter code to change the following options to use a background color of light yellow, a text color of dark green, a border width of 2, a border style of "groove" and disabled state so it can't be edited.

import tkinter as tk

root = tk.Tk()
root.title("Text Widget Questions")

# Create a Text widget
text = tk.Text(root, height=6, width=40, wrap="word", font=("Helvetica", 12))
text.pack(padx=10, pady=10)

# Insert initial content
text.insert(
    "1.0", "Welcome to \nthe Text Widget!\nIt has multiline text.")

# Customize options
text.config(
    bg="light yellow",  # Background color
    fg="dark green",  # Text color
    bd=2,  # Border width
    relief="groove",  # Border style
    insertbackground="dark green",  # Insertion cursor color
    state="disabled",  # Disable editing
    highlightthickness=1,
    highlightcolor="dark green",
    padx=10,
    pady=10,
    yscrollcommand="True",
)

root.mainloop()

3.3. Parameter syntax

text_widget = tk.Text(parent, option=value)
parent is the window or frame object.
Options can be passed as parameters separated by commas.

Parameters:

autoseparators
Syntax: text_widget = tk.Text(parent, autoseparators=1)
Description: Enables automatic separator insertion when typing.
Default: 1
Example: text_widget = tk.Text(root, autoseparators=1)
background
Syntax: text_widget = tk.Text(parent, background="color")
Description: Sets the background color of the text widget.
Default: SystemWindow
Example: text_widget = tk.Text(root, background="lightyellow")
bd
Syntax: text_widget = tk.Text(parent, bd=border_width)
Description: Sets the border width of the text widget.
Default: 1
Example: text_widget = tk.Text(root, bd=2)
bg
Syntax: text_widget = tk.Text(parent, bg="color")
Description: Sets the background color of the text widget.
Default: SystemWindow
Example: text_widget = tk.Text(root, bg="lightyellow")
blockcursor
Syntax: text_widget = tk.Text(parent, blockcursor=0)
Description: Sets the cursor style; a block or normal cursor.
Default: 0
Example: text_widget = tk.Text(root, blockcursor=1)
borderwidth
Syntax: text_widget = tk.Text(parent, borderwidth=width)
Description: Sets the width of the border around the text widget.
Default: 1
Example: text_widget = tk.Text(root, borderwidth=2)
cursor
Syntax: text_widget = tk.Text(parent, cursor="cursor_type")
Description: Sets the mouse cursor when hovering over the text widget.
Default: xterm
Example: text_widget = tk.Text(root, cursor="hand2")
endline
Syntax: text_widget = tk.Text(parent, endline="")
Description: Sets the endline character for new lines.
Default: None
Example: text_widget = tk.Text(root, endline="\n")
exportselection
Syntax: text_widget = tk.Text(parent, exportselection=1)
Description: Allows the text selection to be copied to the clipboard.
Default: 1
Example: text_widget = tk.Text(root, exportselection=1)
fg
Syntax: text_widget = tk.Text(parent, fg="color")
Description: Sets the foreground color (text color) of the text widget.
Default: SystemWindowText
Example: text_widget = tk.Text(root, fg="black")
font
Syntax: text_widget = tk.Text(parent, font=("font_name", size, "style"))
Description: Specifies the font type, size, and style for the text.
Default: TkFixedFont
Example: text_widget = tk.Text(root, font=("Arial", 12, "italic"))
foreground
Syntax: text_widget = tk.Text(parent, foreground="color")
Description: Sets the foreground color (text color) of the text widget.
Default: SystemWindowText
Example: text_widget = tk.Text(root, foreground="black")
height
Syntax: text_widget = tk.Text(parent, height=height_value)
Description: Sets the height of the text widget in lines.
Default: 24
Example: text_widget = tk.Text(root, height=10)
highlightbackground
Syntax: text_widget = tk.Text(parent, highlightbackground="color")
Description: Sets the background color when the text widget does not have focus.
Default: SystemButtonFace
Example: text_widget = tk.Text(root, highlightbackground="gray")
highlightcolor
Syntax: text_widget = tk.Text(parent, highlightcolor="color")
Description: Sets the color of the highlight when the text widget has focus.
Default: SystemWindowFrame
Example: text_widget = tk.Text(root, highlightcolor="blue")
highlightthickness
Syntax: text_widget = tk.Text(parent, highlightthickness=thickness)
Description: Sets the thickness of the highlight border.
Default: 0
Example: text_widget = tk.Text(root, highlightthickness=2)
inactiveselectbackground
Syntax: text_widget = tk.Text(parent, inactiveselectbackground="color")
Description: Sets the background color for selected text when the widget is inactive.
Default: None
Example: text_widget = tk.Text(root, inactiveselectbackground="lightgray")
insertbackground
Syntax: text_widget = tk.Text(parent, insertbackground="color")
Description: Sets the color of the insertion cursor (caret).
Default: SystemWindowText
Example: text_widget = tk.Text(root, insertbackground="red")
insertborderwidth
Syntax: text_widget = tk.Text(parent, insertborderwidth=width)
Description: Sets the width of the border around the insertion cursor.
Default: 0
Example: text_widget = tk.Text(root, insertborderwidth=2)
insertofftime
Syntax: text_widget = tk.Text(parent, insertofftime=milliseconds)
Description: Sets the time the cursor stays off (in milliseconds).
Default: 300
Example: text_widget = tk.Text(root, insertofftime=500)
insertontime
Syntax: text_widget = tk.Text(parent, insertontime=milliseconds)
Description: Sets the time the cursor stays on (in milliseconds).
Default: 600
Example: text_widget = tk.Text(root, insertontime=800)
insertunfocussed
Syntax: text_widget = tk.Text(parent, insertunfocussed="style")
Description: Sets the style of the cursor when the widget is unfocused.
Default: none
Example: text_widget = tk.Text(root, insertunfocussed="underline")
insertwidth
Syntax: text_widget = tk.Text(parent, insertwidth=width)
Description: Sets the width of the insertion cursor.
Default: 2
Example: text_widget = tk.Text(root, insertwidth=5)
maxundo
Syntax: text_widget = tk.Text(parent, maxundo=number)
Description: Sets the maximum number of undo operations.
Default: 0 (unlimited)
Example: text_widget = tk.Text(root, maxundo=100)
padx
Syntax: text_widget = tk.Text(parent, padx=padding_value)
Description: Sets the horizontal padding within the text widget.
Default: 1
Example: text_widget = tk.Text(root, padx=10)
pady
Syntax: text_widget = tk.Text(parent, pady=padding_value)
Description: Sets the vertical padding within the text widget.
Default: 1
Example: text_widget = tk.Text(root, pady=10)
relief
Syntax: text_widget = tk.Text(parent, relief="style")
Description: Sets the border style of the text widget. Options include flat, raised, sunken, groove, ridge.
Default: sunken
Example: text_widget = tk.Text(root, relief="flat")
selectbackground
Syntax: text_widget = tk.Text(parent, selectbackground="color")
Description: Sets the background color of the selected text.
Default: SystemHighlight
Example: text_widget = tk.Text(root, selectbackground="lightblue")
selectborderwidth
Syntax: text_widget = tk.Text(parent, selectborderwidth=width)
Description: Sets the border width of the selection.
Default: 0
Example: text_widget = tk.Text(root, selectborderwidth=1)
selectforeground
Syntax: text_widget = tk.Text(parent, selectforeground="color")
Description: Sets the text color of the selected text.
Default: SystemHighlightText
Example: text_widget = tk.Text(root, selectforeground="white")
setgrid
Syntax: text_widget = tk.Text(parent, setgrid=0)
Description: Enables or disables grid lines in the text widget.
Default: 0
Example: text_widget = tk.Text(root, setgrid=1)
spacing1
Syntax: text_widget = tk.Text(parent, spacing1=spacing_value)
Description: Sets the spacing before paragraphs.
Default: 0
Example: text_widget = tk.Text(root, spacing1=5)
spacing2
Syntax: text_widget = tk.Text(parent, spacing2=spacing_value)
Description: Sets the spacing between lines.
Default: 0
Example: text_widget = tk.Text(root, spacing2=3)
spacing3
Syntax: text_widget = tk.Text(parent, spacing3=spacing_value)
Description: Sets the spacing after paragraphs.
Default: 0
Example: text_widget = tk.Text(root, spacing3=5)
startline
Syntax: text_widget = tk.Text(parent, startline="")
Description: Sets the starting line number for text.
Default: None
Example: text_widget = tk.Text(root, startline=1)
state
Syntax: text_widget = tk.Text(parent, state="state_type")
Description: Sets the state of the text widget. Options include normal, disabled, or hidden.
Default: normal
Example: text_widget = tk.Text(root, state="disabled")
tabs
Syntax: text_widget = tk.Text(parent, tabs=tab_stops)
Description: Sets tab stops for the text widget.
Default: None
Example: text_widget = tk.Text(root, tabs=4)
tabstyle
Syntax: text_widget = tk.Text(parent, tabstyle="style")
Description: Specifies the style for tab stops. Options include tabular.
Default: tabular
Example: text_widget = tk.Text(root, tabstyle="tabular")
takefocus
Syntax: text_widget = tk.Text(parent, takefocus=1)
Description: Allows the text widget to take focus on click.
Default: None
Example: text_widget = tk.Text(root, takefocus=1)
undo
Syntax: text_widget = tk.Text(parent, undo=0)
Description: Enables the undo feature for the text widget.
Default: 0
Example: text_widget = tk.Text(root, undo=1)
width
Syntax: text_widget = tk.Text(parent, width=width_value)
Description: Sets the width of the text widget in characters.
Default: 80
Example: text_widget = tk.Text(root, width=50)
wrap
Syntax: text_widget = tk.Text(parent, wrap="mode")
Description: Sets the text wrapping mode. Options are none, char, or word.
Default: char
Example: text_widget = tk.Text(root, wrap="word")
xscrollcommand
Syntax: text_widget = tk.Text(parent, xscrollcommand=command)
Description: Configures the command for horizontal scrolling.
Default: None
Example: text_widget = tk.Text(root, xscrollcommand=my_xscroll_command)
yscrollcommand
Syntax: text_widget = tk.Text(parent, yscrollcommand=command)
Description: Configures the command for vertical scrolling.
Default: None
Example: text_widget = tk.Text(root, yscrollcommand=my_yscroll_command)