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.
Join the DZone community and get the full member experience.
Join For FreeText 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. 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:
>>> 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:
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:
<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:
>>> 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
:
>>> 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:
>>> 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:
>>> 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
:
>>> 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:
>>> text_widget.delete("1.6")
To delete a range of characters, define these characters just as you would when using get()
.
>>> 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:
>>> 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
Opinions expressed by DZone contributors are their own.
Comments