Tkinter Text Widget

If you want a text box that can take more than one line then the Text widget is the one you want. There's quite a lot you can do with the Text widget, including things like text formatting, background colours, line height, and lots more. We'll only use it to read in text files and do some simple formatting.

Add a text area to your form fromn the previous lesson with this line:

textArea = tk.Text(form, height=12, width=80, wrap=tk.WORD)

Add it just after your form.config line.

The tkinter widget to use, then, is Text. In between the round brackets of Text, we're using four parameters. The first is the form we want the Text widget on. The second and third are height and width parameters. The fourth is the wrap you want to use for your text area. Notice how we've used it:

wrap=tk.WORD

Tkinter has lots of inbuilt constants you can use. Just type tk then a dot. You'll see a popup list of these inbuilt constants. One of them is WORD. This gets you text wrapping between the blank spaces of words. There is a CHAR constant that wraps text on letters in a word. There's also NONE.

Use a pack layout to see your text area:

textArea.pack()

Your code should look like this:

Python code for a Tkinter Text Widget

Run your form to see this:

A Tkinter form with a large text widget

Experiment with the height and width parameters. Change the values and see how it changes the size of your form.

Adding text to a Text area can get quite complicated. A simple way is just to add a string to the insert function of Text. Like this:

textArea.insert('1.0', 'Some default text here')

To add some text to a Text widget, the insert function is used. In between the round brackets of insert, you need where to insert your text. The '1.0' above means line 1 of the text area and character 0. Notice the use of quotations marks (single or double). This is not a float value of 1.0. It's a string.

Instead of specifying a line and character number, you can just use the constant END:

textArea.insert(tk.END, 'Some default text here')

This will add your text from that start character right to the end character.

Run your form and you should see the default text appear.

If you don't like the default font that the text area uses, you can change it. You can do it by using the configure function of the text area:

textArea.configure(font=("Arial", 14, "bold", "italic"))

Between the round brackets of configure, we have this:

font=("Arial", 14, "bold", "italic")

(You don't need to add the bold and italic, if you don't want it.)

So you need the font you want to use, surrounded by quote marks. If you want to override the default font size, add a size of your own.

If you'd like to explore the Text widget in depth, try an internet search using the term tkinter text widget. There's a lot to learn.

For us, though, we just want to open a text file and place the contents into a text area. We're not too worried about formatting.

In the next lesson, you'll learn how to open up a text file with Python code and place the contents into your text widget.

Open a Text File in Python >