Showing messages with messagebox

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

Showing messages with messagebox

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which call shows a pop-up telling the user their quiz score was saved?
A messagebox.showinfo("Saved", "Score saved")
B messagebox.showerror("Saved", "Score saved")
2. A button handler finds the user typed letters in an age box. Which call is the best choice?
A messagebox.showerror("Error", "Age must be a number")
B messagebox.showinfo("Done", "Age must be a number")
3. Which statement about messagebox.showinfo() is correct?
A It takes only a variable name and prints it in the console.
B It takes a title and a message to display.
4. Which line is the correct way to show an error pop-up in tkinter?
A messagebox.showerror("Enter a number")
B messagebox.showerror("Invalid", "Enter a number")
5. What must be done before messagebox can be used in a tkinter program?
A from tkinter import messagebox
B import messagebox.showinfo()

Match the Pairs

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

Left columnRight column
1messagebox.showinfo("Result", "Total saved") Ashowinfo() shows an information icon, which is wrong for invalid input. Fix: use messagebox.showerror("Error", "Invalid age entered") so the user sees an error pop-up.
2messagebox.showerror("Invalid", "Enter a number") Bif not age.isdigit(): messagebox.showerror("Error", "Age must be a number")
3Import messagebox from tkinter so pop-up message functions can be used in a program. CPops up an information window titled Result that tells the user the total was saved.
4A button handler checks the entry text and shows an error pop-up when the input is not a number. Dfrom tkinter import messagebox
5messagebox.showinfo("Your score is 90") Eshowinfo() needs both a title and a message. Only one string is given, so Python raises a TypeError. Fix: messagebox.showinfo("Score", "Your score is 90").
6messagebox.showinfo("Error", "Invalid age entered") FPops up an error window titled Invalid that tells the user their input was wrong.

Definitions

messagebox.showinfo()

Fill in the Blanks

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

1.
messagebox.________("Result", "Saved!")
2.
messagebox._________ ("Error", "Enter a number")
3.
from tkinter import __________
4.
msg = "Saved" messagebox.showinfo("Info", ___)
5.
if ___ age.get().isdigit(): messagebox.showerror("Error", "Digits")

Spot the Error

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

Question 1
A import messagebox from tkinter
B def show():
C t, m = 'Result', 'Saved'
D messagebox.showinfo(t, m)
Question 2
A age = entry.get()
B t, m = 'Error', 'Numbers only'
C if not age.isdigit():
D messagebox.showinfo(t, m)
Question 3
A def show_total():
B total = 5 + 3
C m = 'Total: ' + total
D messagebox.showinfo('Sum', m)
Question 4
A from tkinter import messagebox
B t, m = 'Done', 'File saved'
C messagebox.showInfo(t, m)
Question 5
A def oops():
B showerror(t, m)
C b = Button(win, command=oops)

Answer Key

Showing messages with messagebox
Section A: Quick Questions
Q1
A — messagebox.showinfo("Saved", "Score saved")
Q2
A — messagebox.showerror("Error", "Age must be a number")
Q3
B — It takes a title and a message to display.
Q4
B — messagebox.showerror("Invalid", "Enter a number")
Q5
A — from tkinter import messagebox
Section B: Match the Pairs
Q1
messagebox.showinfo("Result", "Total saved") → Pops up an information window titled Result that tells the user the total was saved.
Q2
messagebox.showerror("Invalid", "Enter a number") → Pops up an error window titled Invalid that tells the user their input was wrong.
Q3
Import messagebox from tkinter so pop-up message functions can be used in a program. → from tkinter import messagebox
Q4
A button handler checks the entry text and shows an error pop-up when the input is not a number. → if not age.isdigit(): messagebox.showerror("Error", "Age must be a number")
Q5
messagebox.showinfo("Your score is 90") → showinfo() needs both a title and a message. Only one string is given, so Python raises a TypeError. Fix: messagebox.showinfo("Score", "Your score is 90").
Q6
messagebox.showinfo("Error", "Invalid age entered") → showinfo() shows an information icon, which is wrong for invalid input. Fix: use messagebox.showerror("Error", "Invalid age entered") so the user sees an error pop-up.
Section C: Definitions
Q1
messagebox.showinfo() — Pops up a message window to tell the user a result. Its partner messagebox.showerror() pops up an error, such as when a handler finds invalid input. Both need a title and a message.
Section D: Fill in the Blanks
Q1
messagebox.showinfo("Result", "Saved!")
Q2
messagebox.showerror ("Error", "Enter a number")
Q3
from tkinter import messagebox
Q4
msg = "Saved" messagebox.showinfo("Info", msg)
Q5
if not age.get().isdigit(): messagebox.showerror("Error", "Digits")
Section E: Spot the Error
Q1
A — This is not valid Python import syntax. It should be 'from tkinter import messagebox' so messagebox.showinfo() can be used.
Q2
D — The input is invalid, so the user should see an error pop-up. Use messagebox.showerror(t, m), not showinfo.
Q3
C — You cannot join a string and an integer with +, so this raises a TypeError. Use an f-string, f'Total: {total}', or str(total).
Q4
C — Python is case-sensitive. The function is showinfo with a lowercase i, so showInfo causes an AttributeError.
Q5
B — Only messagebox was imported, so showerror on its own is not defined (NameError). Call it as messagebox.showerror(t, m).