Programming-Challenges/40 Simple GUI Quiz/landmark_quiz.py

70 lines
2.0 KiB
Python

import tkinter as tk
from tkinter import messagebox
from pathlib import Path
from PIL import Image, ImageTk
ROOT = Path(__file__).resolve().parent
root = tk.Tk()
root.title("Landmark Quiz")
root.geometry("600x560")
root.resizable(False, False)
# Load and resize landmark image
image = Image.open(ROOT / "eiffel_tower.png")
image = image.resize((300, 300))
landmark = ImageTk.PhotoImage(image)
# Window icon
icon = tk.PhotoImage(file=ROOT / "landmark_icon.png")
root.iconphoto(False, icon)
title = tk.Label(root, text="Famous Landmark Quiz", font=("Arial", 20, "bold"))
title.grid(row=0, column=0, columnspan=2, pady=(15, 10))
picture = tk.Label(root, image=landmark)
picture.grid(row=1, column=0, columnspan=2, padx=20, pady=10)
question = tk.Label(
root,
text="Which city is this landmark situated in?",
font=("Arial", 13)
)
question.grid(row=2, column=0, columnspan=2, pady=8)
entry = tk.Entry(root, width=30, font=("Arial", 12))
entry.grid(row=3, column=0, columnspan=2, pady=8)
entry.focus()
result = tk.Label(root, text="", font=("Arial", 12))
result.grid(row=5, column=0, columnspan=2, pady=10)
def check_answer():
answer = entry.get().strip().lower()
if answer == "paris":
result.config(text="Correct! The Eiffel Tower is in Paris.")
messagebox.showinfo("Correct", "Well done! Paris is the correct answer.")
entry.delete(0, tk.END)
else:
result.config(text="That answer is incorrect. Try again.")
messagebox.showerror("Try again", "Incorrect. Please try again.")
entry.select_range(0, tk.END)
entry.focus()
def clear_answer():
entry.delete(0, tk.END)
result.config(text="")
entry.focus()
check_button = tk.Button(root, text="Check answer", command=check_answer, width=16)
check_button.grid(row=4, column=0, padx=10, pady=5)
clear_button = tk.Button(root, text="Clear", command=clear_answer, width=16)
clear_button.grid(row=4, column=1, padx=10, pady=5)
root.bind("<Return>", lambda event: check_answer())
root.mainloop()