A text box is a graphical user-interface widget where users can enter or edit text. In tkinter, the Entry widget creates a single-line text field. Add it to a window by creating it and calling pack(). section 37.3 section 37.0
A text box becomes useful when another part of the program reads its contents:
- Create an Entry widget and place it in the window. section 37.3
- Read the entered value with textbox.get(), then convert or process it as needed. section 37.4
- Connect a button to an event handler so the program responds after the user enters text. section 37.4
Entering a countdown value
A countdown program places an Entry beside instructions and a button. When the button is clicked, the handler reads the text box with int(textbox.get()), uses that number as the loop’s starting value, and displays the changing countdown in a label. section 37.4
Listing 37.2. Adding widgets to the window
import tkinter
window = tkinter.Tk()
window.geometry("800x200")
window.title("My first GUI")
window.configure(background="grey")
red = tkinter.Button(window, text="Red", bg="red") #1
red.pack() #2
yellow = tkinter.Button(window, text="Yellow", bg="yellow")
yellow.pack()
green = tkinter.Button(window, text="Green", bg="green")
green.pack()
textbox = tkinter.Entry(window) #3
textbox.pack() #3
colorlabel = tkinter.Label(window, height="10", width="10") #4
colorlabel.pack() #4
window.mainloop()
See a complete tkinter window containing three buttons, one Entry text box, and one label. section 37.3






