Getting Input with Entry Widgets

00:00 Getting User Input With Entry Widgets When you need to get a little bit of text from a user such as a name or email address, you can use an Entry widget.

00:11 It will display a small text box that the user can type some text into. Creating and styling an entry widget works pretty much like the Label and Button widgets you’ve already seen.

00:22 The code seen on screen creates a widget with a blue background, some yellow text, and a width of 50 text units.

00:39 But styling isn’t the interesting part of Entry widgets. It’s how to use them to get input from a user. There are three main operations you can perform with an Entry widget: retrieving text with .get(), deleting text with .delete(), and inserting text with .insert().

00:59 The best way to get an understanding of entry widgets is to create one and interact with it. Open up a Python shell and enter the code seen on screen. First importing tkinter and creating a new window.

01:18 Now create a Label and an Entry widget. The Label describes the sort of text that should go into the Entry widget.

01:29 It doesn’t enforce any sort of requirements on the entry, but it does tell the user what the program expects them to enter. You need to pack the widgets into the window so that they’re visible.

01:46 On screen you can see what they look like. Notice that Tkinter automatically centers the label above the entry widget in the window. This is a feature of pack(), which you’ll learn about later on in the course.

01:59 Click inside the entry widget with your mouse and type “Real Python”. Now you’ve got some text entered into the entry widget, but that text hasn’t been sent to the program yet, but you can use get() to retrieve the text and assign it to a variable called name.

02:22 You can delete the text as well. The delete() method takes an integer argument that tells Python which character to remove. Here you delete the first character from entry.

02:35 Note that this is zero-indexed just like Python strings. If you need to remove several characters from an entry, then pass a second integer argument to delete() indicating the index of the character where deletion should stop.

02:54 Entry. delete() works just like string slicing. The first argument determines the starting index and the deletion continues up to, but not including, the index passed as the second argument.

03:07 You can use the special constant tk.END for the second argument of delete() to remove all text in an entry.

03:19 And as you can see, the text box is now blank. You can also insert text into an entry widget using insert. The first argument tells insert where to insert the text.

03:39 If there’s no text in entry, then the new text will always be inserted at the beginning of the widget no matter what value you pass as the first argument.

03:48 If entry already contains some text, then insert will insert the new text at the specified position and shift all existing text to the right.

04:07 Entry widgets are great for capturing small amounts of text from a user, but because they’re only displayed on a single line, they’re not ideal for gathering large amounts of text.

04:16 That’s where Text widgets come in and that’s what you’ll be looking at in the next section of the course.

Become a Member to join the conversation.