Skip to content

Commit dbd4e21

Browse files
committed
Add documentation for tkinter.Text widget
1 parent 8e1469c commit dbd4e21

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

Doc/library/tkinter.rst

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,101 @@ Menu indexes (menu.invoke(), menu.entryconfig(), etc.)
963963
labelled ``last``, ``active``, or ``none`` may be interpreted as the above
964964
literals, instead.
965965

966+
.. _tkinter-text-widget:
967+
968+
Text
969+
^^^^
970+
971+
The :class:`tkinter.Text` widget provides a multi-line text editing area
972+
with support for rich features such as text formatting, embedded widgets,
973+
and undo/redo functionality. It is one of the most flexible and powerful
974+
widgets available in Tkinter.
975+
976+
Unlike simpler widgets such as :class:`Entry`, the :class:`tkinter.Text` widget
977+
supports multiple lines of text and allows fine-grained control over the
978+
content using indexes, tags, and marks.
979+
980+
Basic usage::
981+
982+
import tkinter as tk
983+
984+
root = tk.Tk()
985+
text = tk.Text(root)
986+
text.pack()
987+
988+
text.insert("1.0", "Hello, world!")
989+
990+
root.mainloop()
991+
992+
Indexes
993+
-------
994+
995+
Text positions are specified using *index* values. The most common format
996+
is ``line.column``, where *line* and *column* are integers starting from
997+
1 and 0 respectively.
998+
999+
Indexes can also include symbolic names.
1000+
1001+
Examples:
1002+
1003+
* ``"1.0"`` – the beginning of the text
1004+
* ``"end"`` – the end of the text
1005+
* ``"insert"`` – the current insertion cursor position
1006+
1007+
Methods
1008+
-------
1009+
1010+
.. method:: insert(index, chars, *args)
1011+
1012+
Insert *chars* at the given *index*.
1013+
1014+
.. method:: get(index1, index2=None)
1015+
1016+
Return the text between *index1* and *index2*. If *index2* is omitted,
1017+
returns the character at *index1*.
1018+
1019+
.. method:: delete(index1, index2=None)
1020+
1021+
Delete text between *index1* and *index2*.
1022+
1023+
Tags
1024+
----
1025+
1026+
Tags allow you to apply formatting and other attributes to ranges of text.
1027+
1028+
Example::
1029+
1030+
text.tag_add("highlight", "1.0", "1.5")
1031+
text.tag_config("highlight", background="yellow")
1032+
1033+
Tags can control styling such as fonts, colors, and spacing, and can also
1034+
be used to bind events to specific text regions.
1035+
1036+
Marks
1037+
-----
1038+
1039+
Marks represent named positions within the text. They move automatically
1040+
as the text is modified.
1041+
1042+
Example::
1043+
1044+
text.mark_set("my_mark", "1.0")
1045+
1046+
Embedded widgets
1047+
----------------
1048+
1049+
The :class:`tkinter.Text` widget can contain other widgets, such as buttons or images,
1050+
embedded directly within the text flow.
1051+
1052+
Undo and redo
1053+
-------------
1054+
1055+
The widget optionally supports undo and redo operations when configured
1056+
with the ``undo=True`` option.
1057+
1058+
Example::
1059+
1060+
text = tk.Text(root, undo=True)
9661061

9671062
Images
9681063
^^^^^^

0 commit comments

Comments
 (0)