DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Python: How to Use Tkinter's Grid Manager
  • I Was Tired of Flying Blind With AI Agents, So I Built AgentDog
  • Prompt Injection Is Real, So I Built a Python Firewall for LLM Pipelines
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch

Trending

  • Persistent Memory for AI Agents Using LangChain's Deep Agents
  • Amazon Quick: AWS's Agentic Workspace, Explained for Engineers
  • Logging What AI Agents Do in Salesforce: A Simple One-Object Audit Framework
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  1. DZone
  2. Coding
  3. Languages
  4. Python: How to Create Text Widgets Using Tkinter Library

Python: How to Create Text Widgets Using Tkinter Library

In this Python programming tutorial, you will learn how to create text widgets using the Tkinter library for use in GUI-based applications.

By 
DZone Editorial user avatar
DZone Editorial
·
Nov. 19, 24 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
3.2K Views

Join the DZone community and get the full member experience.

Join For Free

Text widgets enable developers to work with multiple lines of text in Python graphical applications. They offer programmers the ability to get, insert, and delete multiple lines of text, unlike with entry boxes, which only work with single line inputs. Just like entry widgets, text widgets use the same set of three methods for getting, inserting, and deleting text. However, their usage is considerably different, since you are now dealing with more than one line of text. 


Programming in Python | Enroll in Free Online Course Today*

*Affiliate link. See Terms of Use.


In this Python programming tutorial, we will learn how to create text widgets using the Tkinter library for use in GUI-based applications.

Creating Text Widgets With Python

The first step you need to know is how to create a text widget. Simply instantiate the Text class in order to do so:

Python
 
>>> import tkinter as tk
>>> window = tk.Tk()
>>> text_widget = tk.Text() # creates text widget
>>> text_widget.pack()


After running the above commands in your Python shell, you will see a large empty text field. This field can not accept text as of yet. If you try typing some letters, you will notice that they are not being shown in it. 

To make it active (that is, ready to accept text), make a mouse click in the text area. 

Next, enter the following text: Tkinter Programming. Put Tkinter on the first line and Programming on the next: 

Screenshot of your screen when you add the Tkinter Programming.


Now, you have managed to put some user text in the widget. Occasionally, you will find that you need to capture this text and pass it to a function. To do this, you will need to use the get() method and then pass the value to a variable.

The usage of get() with the text widget differs from that of the entry widget. You will need to define either one or two parameters in get(). You can define one parameter in case you wish to simply get one character. To capture a range of characters, you will need to define both parameters.

The parameters defined in get() are a two-part combination of the line on which a character is and the index of that character on the line:

Python
 
<line>:<index on line>


Line values begin from 1. Index values begin from 0. 

For example, if you wanted to capture the word Tkinter from the earlier example, you would type the following code:

Python
 
>>> word = text_widget.get(1.0, 1.8)


In other words, the above command captures the character at index 0 on line 1 until the character at index 7 on line 1. It is important for you to note that get() does not capture the character at the ending index indicated in the second argument - it stops at the index before.

If you want to instead capture all the text (all the lines of text) in the widget, you would have to use the special character tk.END:

Python
 
>>> text1 = text_widget.get( 1.0, tk.END)


Read: Python for Beginners: An Introductory Guide to Getting Started

Inserting Text into a Widget in Python

To insert text in the widget, you need to use insert(). This method takes in two parameters. The first is <line>:<index on line> just like in get(). The second is the string to be inserted. 

For example, the command below inserts the string Python at the beginning of the first line. If there is any text at the starting index, it will be pushed to the right:

Python
 
>>> text_widget.insert( "1.0", "Python")


If you wanted to add the text to a newline, then you would do it the same way as above. However, you would need to add a newline character to your string. Otherwise, the text will be appended to the text on the previous line:

Python
 
>>> text2 = text_widget.insert( "1:0", "nProgram")


Sometimes you may need to add text at the end of the text you currently have. You may find it disturbing to always have to keep track of the last index. To simplify your work, you can instead use the special character tk.END:

Python
 
>>> text_widget.insert ( tk.END,  "Text at the end")


Deleting Text from a Text Widget in Python

To delete text, you need to use the delete() method. If you want to delete a single character, then specify the : pair, as shown below:

Python
 
>>> text_widget.delete("1.6")


To delete a range of characters, define these characters just as you would when using get().

Python
 
>>> text_widget.delete("1.0", "1.9")


In case you wish to clear out the whole text box, then use tk.END as your second argument:

Python
 
>>> text_widget.delete("1.0", tk.END)


Read: Modern Python: Patterns, Features, and Strategies for Writing Efficient Code (Part 1)

Conclusion to Creating Text Widgets with Tkinter

Python text widget enables your application to capture and use multiple lines of text, unlike the entry widget. Therefore, you should use it in case you expect a multiline text from your application users. As a reminder, remember to place the newline character (n) in case you are inserting text on a new line in your text box. Read more: The Best Python Libraries for Data Science and Machine Learning

Python (language) Tkinter

Opinions expressed by DZone contributors are their own.

Related

  • Python: How to Use Tkinter's Grid Manager
  • I Was Tired of Flying Blind With AI Agents, So I Built AgentDog
  • Prompt Injection Is Real, So I Built a Python Firewall for LLM Pipelines
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook