{"id":1253,"date":"2020-11-26T00:56:32","date_gmt":"2020-11-26T00:56:32","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1253"},"modified":"2025-04-04T01:15:44","modified_gmt":"2025-04-04T01:15:44","slug":"tkinter-text","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-text\/","title":{"rendered":"Tkinter Text"},"content":{"rendered":"\r\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Tkinter <code>Text<\/code> widget to add a text editor to your application.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='introduction-to-the-tkinter-text-widget'>Introduction to the Tkinter Text widget <a href=\"#introduction-to-the-tkinter-text-widget\" class=\"anchor\" id=\"introduction-to-the-tkinter-text-widget\" title=\"Anchor for Introduction to the Tkinter Text widget\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>The <code>Text<\/code> widget allows you to display and edit multi-line text areas with various styles. Besides plain text, the <code>Text<\/code> widget supports embedded images, links, and custom formatting.<\/p>\r\n\r\n\r\n\r\n<p>To create a text widget, you follow these steps:<\/p>\r\n\r\n\r\n\r\n<p>First, import <code>tkinter<\/code> as <code>tk<\/code>:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Second, create a new <code>Text<\/code> widget using the <code>Text()<\/code> constructor:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">text = tk.Text(master=None, conf={}, **kw)<\/code><\/span><\/pre>\r\n\r\n\r\n<p>In this syntax:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><code>master<\/code> : The parent <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-window\/\">window<\/a> of the <code>Text<\/code> widget.<\/li>\r\n\r\n\r\n\r\n<li><code>cnf<\/code> : A <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-dictionary\/\">dictionary<\/a> that includes the widget&#8217;s configurations.<\/li>\r\n\r\n\r\n\r\n<li><code>kw<\/code>: one or more <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-keyword-arguments\/\">keyword arguments<\/a> used to configure the <code>Text<\/code> widget.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p class=\"note\">The <code>Text<\/code> widget is only available in the <code>tkinter<\/code> module, not the <code>tkinter.ttk<\/code> module.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='creating-a-simple-text-widget'>Creating a Simple Text Widget <a href=\"#creating-a-simple-text-widget\" class=\"anchor\" id=\"creating-a-simple-text-widget\" title=\"Anchor for Creating a Simple Text Widget\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>The following example creates a <code>Text<\/code> widget with eight rows and places it on the main window:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n\r\nroot = tk.Tk()\r\nroot.title(<span class=\"hljs-string\">\"Text Widget Example\"<\/span>)\r\n\r\ntext = tk.Text(root, height=<span class=\"hljs-number\">8<\/span>)\r\ntext.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>, expand=True,fill=tk.BOTH)\r\n\r\n\r\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Output:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"670\" height=\"188\" class=\"wp-image-7451\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget.png\" alt=\"tkinter Text widget\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget.png 670w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-300x84.png 300w\" sizes=\"auto, (max-width: 670px) 100vw, 670px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>In this example:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><code>height=8<\/code> specifies the number of rows in the <code>Text<\/code> widget.<\/li>\r\n\r\n\r\n\r\n<li><code>padx<\/code> and <code>pady<\/code> add padding around the Text widget.<\/li>\r\n\r\n\r\n\r\n<li><code>expand=True<\/code> and <code>fill=tk.BOTH<\/code> expand and fill available space.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='adding-content-to-the-text-widget'>Adding content to the Text widget <a href=\"#adding-content-to-the-text-widget\" class=\"anchor\" id=\"adding-content-to-the-text-widget\" title=\"Anchor for Adding content to the Text widget\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>To insert contents into the Text widget, you use the <code>insert()<\/code> method. For example:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n\r\nroot = tk.Tk()\r\nroot.title(<span class=\"hljs-string\">\"Text Widget Example\"<\/span>)\r\n\r\ntext = tk.Text(root, height=<span class=\"hljs-number\">8<\/span>)\r\ntext.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>, expand=True,fill=tk.BOTH)\r\n\r\ntext.insert(\r\n    index=<span class=\"hljs-string\">'1.0'<\/span>, \r\n    chars= <span class=\"hljs-string\">'This is a Text widget demo'<\/span>\r\n)\r\n\r\n\r\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Output:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"670\" height=\"188\" class=\"wp-image-7450\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-adding-text.png\" alt=\"tkinter Text widget - adding text\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-adding-text.png 670w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-adding-text-300x84.png 300w\" sizes=\"auto, (max-width: 670px) 100vw, 670px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>The <code>insert()<\/code> method takes two arguments:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><code>index<\/code> &#8216;1.0&#8217; the first character of the first line.<\/li>\r\n\r\n\r\n\r\n<li><code>chars<\/code>: the text content that the <code>Text<\/code> widget inserts at the specified position.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p>The <code>index<\/code> has the following format:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-string\">'line.column'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>For example, <code>'1.0'<\/code> is the first character of the first line.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='retrieving-text-from-the-text-widget'>Retrieving Text from the Text widget <a href=\"#retrieving-text-from-the-text-widget\" class=\"anchor\" id=\"retrieving-text-from-the-text-widget\" title=\"Anchor for Retrieving Text from the Text widget\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>To retrieve the contents of a <code>Text<\/code> widget, you use its <code>get()<\/code> method. For example:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\r\n<span class=\"hljs-keyword\">from<\/span> tkinter.messagebox <span class=\"hljs-keyword\">import<\/span> showinfo\r\n\r\nroot = tk.Tk()\r\nroot.title(<span class=\"hljs-string\">\"Text Widget Example\"<\/span>)\r\n\r\ntext = tk.Text(root, height=<span class=\"hljs-number\">8<\/span>)\r\ntext.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>, expand=True,fill=tk.BOTH)\r\n\r\ntext.insert(\r\n    index=<span class=\"hljs-string\">'1.0'<\/span>, \r\n    chars= <span class=\"hljs-string\">'This is a Text widget demo'<\/span>\r\n)\r\n\r\nbutton = ttk.Button(\r\n    root,\r\n    text=<span class=\"hljs-string\">'Get Text'<\/span>,\r\n    command=lambda: showinfo(\r\n        title=<span class=\"hljs-string\">'Text Data'<\/span>,\r\n        message=text.get(<span class=\"hljs-string\">'1.0'<\/span>, tk.END)\r\n    )\r\n)\r\n\r\nbutton.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>, side=tk.LEFT)\r\n\r\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>The <code>get()<\/code> method accepts two arguments <code>index1<\/code> and <code>index2<\/code> that specify the starting and ending index you want to retrieve the contents.<\/p>\r\n\r\n\r\n\r\n<p>In this example, we retrieve all contents starting from the first character of line one (<code>'1.0'<\/code>) till the end (<code>tk.END<\/code>).<\/p>\r\n\r\n\r\n\r\n<p>Output:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"670\" height=\"233\" class=\"wp-image-7452\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-getting-text.png\" alt=\"tkinter Text widget - getting text\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-getting-text.png 670w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-getting-text-300x104.png 300w\" sizes=\"auto, (max-width: 670px) 100vw, 670px\" \/><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='disabling-and-enabling-the-text-widget'>Disabling and Enabling the Text widget <a href=\"#disabling-and-enabling-the-text-widget\" class=\"anchor\" id=\"disabling-and-enabling-the-text-widget\" title=\"Anchor for Disabling and Enabling the Text widget\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>To prevent users from changing the contents of a <code>Text<\/code> widget, you can disable it by setting the <code>state<\/code> option to <code>'disabled'<\/code> like this:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">text&#91;<span class=\"hljs-string\">'state'<\/span>] = <span class=\"hljs-string\">'disabled'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>To re-enable editing, you can change the <code>state<\/code> option back to <code>normal<\/code>:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">text&#91;<span class=\"hljs-string\">'state'<\/span>] = <span class=\"hljs-string\">'normal'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='deleting-contents-from-the-text-widget'>Deleting contents from the Text widget <a href=\"#deleting-contents-from-the-text-widget\" class=\"anchor\" id=\"deleting-contents-from-the-text-widget\" title=\"Anchor for Deleting contents from the Text widget\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>To delete text from the widget, you can use the <code>delete()<\/code> method:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">text.delete(<span class=\"hljs-string\">'1.0'<\/span>, tk.END)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>For example:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\r\n<span class=\"hljs-keyword\">from<\/span> tkinter.messagebox <span class=\"hljs-keyword\">import<\/span> showinfo\r\n\r\nroot = tk.Tk()\r\nroot.title(<span class=\"hljs-string\">\"Text Widget Example\"<\/span>)\r\n\r\ntext = tk.Text(root, height=<span class=\"hljs-number\">8<\/span>)\r\ntext.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>, expand=True,fill=tk.BOTH)\r\n\r\ntext.insert(\r\n    index=<span class=\"hljs-string\">'1.0'<\/span>, \r\n    chars= <span class=\"hljs-string\">'This is a Text widget demo'<\/span>\r\n)\r\n\r\nget_button = ttk.Button(\r\n    root,\r\n    text=<span class=\"hljs-string\">'Get Text'<\/span>,\r\n    command=lambda: showinfo(\r\n        title=<span class=\"hljs-string\">'Text Data'<\/span>,\r\n        message=text.get(<span class=\"hljs-string\">'1.0'<\/span>, tk.END)\r\n    )\r\n)\r\nget_button.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>, side=tk.LEFT)\r\n\r\n\r\nclear_button = ttk.Button(\r\n    root,\r\n    text=<span class=\"hljs-string\">'Clear All Text'<\/span>,\r\n    command=lambda: text.delete(<span class=\"hljs-string\">'1.0'<\/span>, tk.END)\r\n)\r\n\r\nclear_button.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>, side=tk.RIGHT)\r\n\r\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Output:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"670\" height=\"233\" class=\"wp-image-7448\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-deleting-text.png\" alt=\"tkinter Text widget - deleting text\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-deleting-text.png 670w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-deleting-text-300x104.png 300w\" sizes=\"auto, (max-width: 670px) 100vw, 670px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>In this example, the <code>delete()<\/code> function clears all text from the widget by specifying the range <code>'1.0'<\/code> to <code>tk.END<\/code>.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='changing-the-appearance-of-the-text-widget'>Changing the appearance of the Text widget <a href=\"#changing-the-appearance-of-the-text-widget\" class=\"anchor\" id=\"changing-the-appearance-of-the-text-widget\" title=\"Anchor for Changing the appearance of the Text widget\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>Here are some configurations you can use to change the appearance of the Text widget:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><code>bg<\/code>: The background color<\/li>\r\n\r\n\r\n\r\n<li><code>fg<\/code>: The text color<\/li>\r\n\r\n\r\n\r\n<li><code>insertBackground<\/code>: the cursor color<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p>For example, the following program creates a <code>Text<\/code> widget with a dark background and white text:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\r\n<span class=\"hljs-keyword\">from<\/span> tkinter.messagebox <span class=\"hljs-keyword\">import<\/span> showinfo\r\n\r\nroot = tk.Tk()\r\nroot.title(<span class=\"hljs-string\">\"Text Widget Example\"<\/span>)\r\n\r\ntext = tk.Text(root, height=<span class=\"hljs-number\">8<\/span>)\r\ntext.config(\r\n    font=(<span class=\"hljs-string\">\"Consolas\"<\/span>, <span class=\"hljs-number\">12<\/span>), \r\n    fg=<span class=\"hljs-string\">\"#F0F0F0\"<\/span>, \r\n    bg=<span class=\"hljs-string\">\"#282C34\"<\/span>, \r\n    insertbackground=<span class=\"hljs-string\">\"white\"<\/span>\r\n)\r\n\r\ntext.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>, expand=True,fill=tk.BOTH)\r\ntext.insert(\r\n    index=<span class=\"hljs-string\">'1.0'<\/span>, \r\n    chars= <span class=\"hljs-string\">'This is a Text widget demo'<\/span>\r\n)\r\n\r\n\r\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Output:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"212\" class=\"wp-image-7447\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-with-dark-background.png\" alt=\"tkinter Text widget with dark background\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-with-dark-background.png 750w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-with-dark-background-300x85.png 300w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='embedding-links'>Embedding links <a href=\"#embedding-links\" class=\"anchor\" id=\"embedding-links\" title=\"Anchor for Embedding links\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>You can embed links into the <code>Text<\/code> widget using the <code>tag_add<\/code> method and change the link appearance using the <code>tag_config<\/code> method.<\/p>\r\n\r\n\r\n\r\n<p>For example, the following program embeds a link to the <code>Text<\/code> widget:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">import tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\nimport webbrowser\r\n\r\n\r\nroot = tk.Tk()\r\nroot.title(<span class=\"hljs-string\">\"Text Widget Example\"<\/span>)\r\n\r\ntext = tk.Text(root, height=<span class=\"hljs-number\">8<\/span>)\r\ntext.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>, expand=<span class=\"hljs-keyword\">True<\/span>,fill=tk.BOTH)\r\n\r\ntext.insert(<span class=\"hljs-string\">\"1.0\"<\/span>, <span class=\"hljs-string\">\"Click here to visit pythontutorial.net\"<\/span>)\r\n\r\n<span class=\"hljs-comment\"># add link<\/span>\r\ntext.tag_add(<span class=\"hljs-string\">\"link\"<\/span>, <span class=\"hljs-string\">\"1.0\"<\/span>, <span class=\"hljs-string\">\"1.10\"<\/span>)\r\ntext.tag_config(<span class=\"hljs-string\">\"link\"<\/span>, foreground=<span class=\"hljs-string\">\"blue\"<\/span>, underline=<span class=\"hljs-keyword\">True<\/span>)\r\n\r\n<span class=\"hljs-comment\"># link click<\/span>\r\ntext.tag_bind(\r\n    <span class=\"hljs-string\">\"link\"<\/span>, \r\n    <span class=\"hljs-string\">\"&lt;Button-1&gt;\"<\/span>, \r\n    lambda e: webbrowser.open(<span class=\"hljs-string\">\"https:\/\/www.pythontutorial.net\"<\/span>)\r\n)\r\n\r\n<span class=\"hljs-comment\"># link hover<\/span>\r\ntext.tag_bind(\r\n    <span class=\"hljs-string\">\"link\"<\/span>, \r\n    <span class=\"hljs-string\">\"&lt;Enter&gt;\"<\/span>, \r\n    lambda e: text.config(cursor=<span class=\"hljs-string\">\"hand2\"<\/span>)\r\n)\r\n\r\n<span class=\"hljs-comment\"># link leave<\/span>\r\ntext.tag_bind(\r\n    <span class=\"hljs-string\">\"link\"<\/span>, \r\n    <span class=\"hljs-string\">\"&lt;Leave&gt;\"<\/span>, \r\n    lambda e: text.config(cursor=<span class=\"hljs-string\">\"\"<\/span>)\r\n)\r\n\r\n<span class=\"hljs-comment\"># show the Text editor on the main window<\/span>\r\ntext.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>, side=tk.LEFT)\r\n\r\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Output:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"670\" height=\"188\" class=\"wp-image-7453\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-adding-links.png\" alt=\"tkinter Text widget - adding links\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-adding-links.png 670w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-adding-links-300x84.png 300w\" sizes=\"auto, (max-width: 670px) 100vw, 670px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>How it works.<\/p>\r\n\r\n\r\n\r\n<p>First, adding a link to the text on line 1 from the first to the 10th characters (<code>Click here<\/code>):<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">text.tag_add(<span class=\"hljs-string\">\"link\"<\/span>, <span class=\"hljs-string\">\"1.0\"<\/span>, <span class=\"hljs-string\">\"1.10\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Second, change the text color of the link to <code>blue<\/code> and underline it:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">text.tag_config(<span class=\"hljs-string\">\"link\"<\/span>, foreground=<span class=\"hljs-string\">\"blue\"<\/span>, underline=<span class=\"hljs-keyword\">True<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Third, open the url <code>https:\/\/www.pythontutorial.net<\/code> using the web browser when users click the link:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">text.tag_bind(\r\n    <span class=\"hljs-string\">\"link\"<\/span>, \r\n    <span class=\"hljs-string\">\"&lt;Button-1&gt;\"<\/span>, \r\n    lambda e: webbrowser.open(<span class=\"hljs-string\">\"https:\/\/www.pythontutorial.net\"<\/span>)\r\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>The <code>\"&lt;Button-1&gt;\"<\/code> traps the left mouse click.<\/p>\r\n\r\n\r\n\r\n<p>Fourth, change the cursor to <code>hand2<\/code> when the mouse hovers over the link:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">text.tag_bind(\r\n    <span class=\"hljs-string\">\"link\"<\/span>, \r\n    <span class=\"hljs-string\">\"&lt;Enter&gt;\"<\/span>, \r\n    lambda e: text.config(cursor=<span class=\"hljs-string\">\"hand2\"<\/span>)\r\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Fifth, change the cursor back when the mouse leaves the link:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">text.tag_bind(\r\n    <span class=\"hljs-string\">\"link\"<\/span>, \r\n    <span class=\"hljs-string\">\"&lt;Leave&gt;\"<\/span>, \r\n    lambda e: text.config(cursor=<span class=\"hljs-string\">\"\"<\/span>)\r\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='embedding-images'>Embedding Images <a href=\"#embedding-images\" class=\"anchor\" id=\"embedding-images\" title=\"Anchor for Embedding Images\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>To embed an image into the Text&#8217;s content, you use the image_create method. For example:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">import tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n\r\nroot = tk.Tk()\r\nroot.title(<span class=\"hljs-string\">\"Text Widget Example\"<\/span>)\r\n\r\ntext = tk.Text(root, height=<span class=\"hljs-number\">8<\/span>)\r\ntext.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>, expand=<span class=\"hljs-keyword\">True<\/span>,fill=tk.BOTH)\r\n\r\ntext.insert(index=<span class=\"hljs-string\">'1.0'<\/span>, chars= <span class=\"hljs-string\">'This is a Text widget demo'<\/span>)\r\n\r\n<span class=\"hljs-comment\"># embed an image<\/span>\r\nimage = tk.PhotoImage(file=<span class=\"hljs-string\">\".\/assets\/python.png\"<\/span>) \r\ntext.image_create(<span class=\"hljs-string\">\"1.0\"<\/span>, image=image)\r\n\r\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Output:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"670\" height=\"188\" class=\"wp-image-7459\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-embedding-images.png\" alt=\"tkinter Text widget - embedding images\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-embedding-images.png 670w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/tkinter-Text-widget-embedding-images-300x84.png 300w\" sizes=\"auto, (max-width: 670px) 100vw, 670px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>How it works.<\/p>\r\n\r\n\r\n\r\n<p>First, create a new <code><a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-photoimage\/\">PhotoImage<\/a><\/code> with the path to the image:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">image = tk.PhotoImage(file=<span class=\"hljs-string\">\".\/assets\/python.png\"<\/span>) <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>You need to replace the image path with your actual one.<\/p>\r\n\r\n\r\n\r\n<p>Second, embed the image at the beginning of the line 1 (<code>1.0<\/code>)<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">text.image_create(<span class=\"hljs-string\">\"1.0\"<\/span>, image=image)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>Use Tkinter <code>Text<\/code> widget to create a multi-line text area.<\/li>\r\n<\/ul>\r\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1253\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-text\/\"\n\t\t\t\tdata-post-title=\"Tkinter Text\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1253\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-text\/\"\n\t\t\t\tdata-post-title=\"Tkinter Text\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the Tkinter Text widget to add a text editor to your application.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":13,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1253","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=1253"}],"version-history":[{"count":5,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1253\/revisions"}],"predecessor-version":[{"id":7462,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1253\/revisions\/7462"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1232"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=1253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}