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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Python: How to Use Tkinter's Grid Manager
  • Start Coding With Google Cloud Workstations
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Why I Started Using Dependency Injection in Python

Trending

  • Top Book Picks for Site Reliability Engineers
  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  • Unlocking the Benefits of a Private API in AWS API Gateway
  • Google Cloud Document AI Basics
  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
1.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
  • Start Coding With Google Cloud Workstations
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Why I Started Using Dependency Injection in Python

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!