GUI Entry control and .get()

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

GUI Entry control and .get()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line creates a text box inside the window called root?
A entry = tk.Label(root)
B entry = tk.Entry(root)
2. Which line reads what the user typed into the text box called entry?
A text = entry.get()
B text = entry.read()
3. A user types 25 into an Entry. What data type does entry.get() return?
A An integer: 25
B A string: '25'
4. Which line correctly turns the typed text into a whole number?
A age = int(entry.get())
B age = entry.get(int)
5. Which line clears everything from the text box called entry?
A entry.delete(0, tk.END)
B entry.get(0, tk.END)

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1entry = tk.Entry(root) AThe brackets are missing, so this stores the method itself instead of calling it. It should be entry.get()
2name = entry.get() Bentry.delete(0, tk.END)
3Clear the text box by deleting every character from the first position to the end CCreates a text box inside the window called root and stores it in the variable entry
4The text from an Entry is a string, so convert it to an integer before doing arithmetic DReads the text the user typed in the text box and stores it as a string in name
5total = entry.get() + 10 Eget() returns a string, so adding the number 10 causes a TypeError. Convert first: total = int(entry.get()) + 10
6text = entry.get Fage = int(entry.get())

Definitions

entry = tk.Entry(root)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
entry = tk._____(root)
2.
name = entry.___()
3.
entry.______(0, tk.END)
4.
entry.delete(0, tk.___)
5.
text = entry.get() price = _____(text)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A root = tk.Tk()
B entry = tk.Entry(root)
C entry.pack()
D name = entry.get
Question 2
A entry = tk.Entry(root)
B text = entry.text()
C age = int(text)
D print(age + 1)
Question 3
A entry = tk.entry(root)
B entry.pack()
C value = entry.get()
Question 4
A entry = tk.Entry(root)
B entry.pack()
C entry.clear()
D entry.focus()
Question 5
A def show():
B n = float(entry)
C label.config(text=n * 2)

Answer Key

GUI Entry control and .get()
Section A: Quick Questions
Q1
B — entry = tk.Entry(root)
Q2
A — text = entry.get()
Q3
B — A string: '25'
Q4
A — age = int(entry.get())
Q5
A — entry.delete(0, tk.END)
Section B: Match the Pairs
Q1
entry = tk.Entry(root) → Creates a text box inside the window called root and stores it in the variable entry
Q2
name = entry.get() → Reads the text the user typed in the text box and stores it as a string in name
Q3
Clear the text box by deleting every character from the first position to the end → entry.delete(0, tk.END)
Q4
The text from an Entry is a string, so convert it to an integer before doing arithmetic → age = int(entry.get())
Q5
total = entry.get() + 10 → get() returns a string, so adding the number 10 causes a TypeError. Convert first: total = int(entry.get()) + 10
Q6
text = entry.get → The brackets are missing, so this stores the method itself instead of calling it. It should be entry.get()
Section C: Definitions
Q1
entry = tk.Entry(root) — Creates a text box in the window. Call entry.get() to read what the user typed. It is always a string, so convert it with int() or float() to use it as a number.
Section D: Fill in the Blanks
Q1
entry = tk.Entry(root)
Q2
name = entry.get()
Q3
entry.delete(0, tk.END)
Q4
entry.delete(0, tk.END)
Q5
text = entry.get() price = float(text)
Section E: Spot the Error
Q1
D — get is a method, so it needs brackets to run. Without them, name just stores the method itself instead of the text. It should be name = entry.get().
Q2
B — An Entry has no text() method. The text is read with entry.get(). It should be text = entry.get().
Q3
A — Python is case-sensitive and the widget class is Entry with a capital E. tk.entry does not exist. It should be entry = tk.Entry(root).
Q4
C — A tkinter Entry has no clear() method. To empty the box, delete from index 0 to the end. It should be entry.delete(0, tk.END).
Q5
B — float() cannot convert the Entry widget itself. You must convert the text it holds. It should be n = float(entry.get()).