Home Python Course Menu Book

Python Tkinter Text Entry

There are two tkinter widgets that you can use for text boxes. One is called the Entry widget and the other the Text widget. The Entry widget is used for single line text boxes. The Text widget is quite a complex multiline text area you can use to format text in a wide variety of styles. We'll explore the Text widget when we cover file manipulation in a later section. Let's have a look at the Entry widget.

You can either delete your label code from the previous lesson, or start a new project. For the first three lines, add these:

import tkinter as tk
form = tk.Tk()
form.title("Entry Text Boxes")

Now add a mainloop at the end:


Instead of using pack as the layout manager, as we did for the labels, we'll use grid. What we'll do is to have a label for first name then an entry text box. On a second row, we'll then have a label and entry box for a surname. We want to create a layout like this:

Tkinter form with two entry text boxes

Just before your mainloop line, add a simple label:

firstLabel = tk.Label(form, text="First Name:")

To add an entry text box, type this line:

firstEntry = tk.Entry(form)

To get an entry text box, then, you use the keyword Entry. Between the round brackets of Entry, you need the name of a parent widget, the form in our case. Just like the Label widget, you can add parameters after the name of your parent widget. We'll leave them off, for now.

Your code should look like this:

Python code for a Tkinter Entry text box

Now add a second label and entry text box with these lines:

surnameLabel = tk.Label(form, text="Surname:")
surnameEntry = tk.Entry(form)

And here's your code now:

Python code for two entry text boxes

The grid works with rows and columns. Think of it like a spreadsheet. The upper left cell is row 0, column 0. The next cell along is row 0, column 1. To add a second row, you'd have row 1, column 0. Like this:

The Tkinter grid layout

Our first name label, then would go in row 0, column 0. The first name entry box would go next to it in row 0, column 1. The surname label goes in row 1, column 0, and its entry box goes in row 1, column 1.

Add this line to your code

firstLabel.grid(row=0, column=0):

And this one:

firstEntry.grid(row=0, column=1)

Your code should look like this:

Python code for a Tkinter grid layout

Instead of using pack, as we did for the previous lesson, we're now using grid. In between the round brackets of grid, we're specifying the row and column we want to place that particular widget in.

Add the code for the other two widgets:

surnameLabel.grid(row=1, column=0)
surnameEntry.grid(row=1, column=1)

And here's what it should look like in PyCharm:

Python code for a Tkinter text entry form

Run your program and take a look at your form:

A Tkinter form showing a basic grid layout

As you can see, the controls are all squashed up into the top left. You can add space, though.

The grid layout has padx and pady parameters you can use. The x means from left to right while the y means from top to bottom. (So pady means add space in the y direction and is not a slight to Irishmen everywhere!) You add padx and pady between the round brackets of grid. Change your first label line to this:

firstLabel.grid(row=0, column=0, padx=15, pady=15)

Add padx and pady parameters to the rest of your grid items:

firstEntry.grid(row=0, column=1, padx=15, pady=15)
surnameLabel.grid(row=1, column=0, padx=15, pady=15)
surnameEntry.grid(row=1, column=1, padx=15, pady=15)

Run your program again and you should find that the layout has more room:

Tkinter form showing the effects of padx and pady

You can have different styles of entry boxes. You do this with the relief parameter. Try it out on your entry boxes. The values you can have for relief are flat, groove, raised, ridge and sunken. The default is sunken.

surnameEntry = tk.Entry(form, relief="raised")

Here's what the raised entry box looks like. Compare it to the one above, which is sunken:

Tkinter entry boxes with a sunken look

We'll add a button soon. When the button is clicked, we'll take the first name and surname, join them together and place them into a third entry box. For this, you'll need a third entry box. Add this one for yourself, along with its grid position. Your form should look like this, when you're done:

Three Tkinter entry boxes on a form

In the next lesson, we'll take a look at the Tkinter button widget.