Getting Multiline User Input
00:00
Getting Multiline User Input With Text
Widgets. Text
widgets are used for entering text just like Entry
widgets. The difference is that text widgets may contain multiple lines of text.
00:13
With a text widget, user can input a whole paragraph or even several pages of text. As with Entry
widgets, you can perform three main operations on them.
00:23
Retrieve text with get()
, delete text with delete()
and insert text with insert()
. Although the method names are the same as the Entry
methods, they work slightly differently so let’s take a look at them.
00:35 In the Python shell create a new blank window and pack a text widget into it.
00:55
As you can see on screen, text boxes are much larger than Entry
widgets by default. Click anywhere inside the window to activate the text_box
and then you can start typing.
01:06 You can type some things such as “Hello” and then “World” and you should see the text appear on screen.
01:16
Just as with Entry
widgets, You can retrieve text using get()
, but calling get()
with no arguments doesn’t return the full text in the text box as it does with Entry
widgets.
01:27
It raises an exception. For Text
widgets, get()
requires at least one argument. Calling with a single index returns a single character.
01:38
To retrieve several characters, you need to pass a start index and an end index. Indices in Text
widgets work differently than in Entry
widgets.
01:50
Since Text
widgets can have several lines of text an index must contain two pieces of information. Firstly, the line number of a character Secondly the position of the character on that line.
02:02
Line numbers start with one and character positions start with zero. To make an index, you create a string of the form seen on screen, replacing line with the <line>
number and <char>
with the character number.
02:15 For example, “1.0” represents the first character on the first line and “2.3” represents the fourth character on the second line. Use index “1.0” to get the first letter from the text box you created earlier.
02:36 Just as with Python string slices, in order to get the entire word ‘Hello’ from the text box, the text index must be one more than the index of the last character to be read.
02:46 So to get the word ‘Hello’ from the text box, use “1.0” for the first index and “1.5” for the second index. To
02:59 to get the word ‘World’ on the second line of the text box, change the line numbers in each index to
03:09
“2.0”. To get all of the text in a text box, set the starting index to “1.0” and use the special tk.END
constant for the second index.
03:22
Notice that text returned by get()
includes any new line characters. You can also see on screen that every line in a Text
widget has a new line character at the end, including the last line in the text_box.
03:38
Delete()
is used to delete characters from a text_box. It works just like delete()
for Entry
widgets, and there are two ways to use it: with a single argument or with two arguments.
03:50
Using the single argument version, you pass to delete()
the index of a single character to be deleted. This will delete the first character H
from the text_box.
04:07
With the two argument version, you pass two indices to delete()
a range of characters starting at the first index and up to, but not including the second index.
04:17
To delete the remaining lo
on the first line of the text box, use the code seen on screen.
04:28 Notice that the text is gone from the first line leaving a blank line followed by the word ‘World’ on the second line. Even though you can’t see it, there’s still a character on the first line and it’s a new line character.
04:42
You can verify this using get()
.
04:49 If you delete that character, then the rest of the contents of the text_box will shift up a line. Now you can see that ‘World’ is on the first line. Try to clear out the rest of the text box.
05:02
Set 1.0
as the start index and use tk.END
for the second index. The text_box is now empty. You can insert text into a text_box using insert()
. This inserts the word “Hello” at the beginning of the text box using the same line column format used by get()
to specify the insertion position.
05:28 Check out what happens when you try to insert the word “World” on the second line.
05:39 Instead of inserting the text on the second line, it’s inserted at the end of the first line. If you want to insert text onto a new line, then you need to insert a new line character manually into the string being inserted.
06:05
Insert()
will do one of two things: insert text at the specified position if there’s already text at or after that position, or append text to the specified line if the character number is greater than the index of the last character in the text_box.
06:21
It’s usually impractical to try and keep track of what index the last character is. The best way to insert text at the end of a Text
widget is to pass tk.END
to the first parameter of insert()
.
06:36 Don’t forget to include the new line character at the beginning of the text if you want to put it on a new line.
06:46
Label
, Button
, Entry
and Text
widgets are just a few of the widgets available in tkinter
.
06:53 There are several others including widgets for check buttons, radio buttons, scroll bars, and progress bars. To start exploring these, take a look at the More Widgets tutorial at the link seen on screen.
07:07
In the next section of the course, you’ll see how you can organize the layout of your windows with the Frame
widget.
Become a Member to join the conversation.