Advertisement
CodeCrusader

Quiz App GUI (Create Your Own Questions)

Jun 10th, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | Source Code | 0 0
  1. import tkinter as tk
  2.  
  3. # Initialize the window
  4. window = tk.Tk()
  5. window.title("Quiz Game")
  6.  
  7. # Quiz questions and answers
  8. questions = [
  9.     "What is the capital of France?",
  10.     "Which planet is known as the Red Planet?",
  11.     "Who painted the Mona Lisa?"
  12. ]
  13. answers = [
  14.     "Paris",
  15.     "Mars",
  16.     "Leonardo da Vinci"
  17. ]
  18.  
  19. # Variables for tracking the current question and score
  20. current_question = 0
  21. score = 0
  22.  
  23. # Function to display the current question
  24. def display_question():
  25.     question_label.config(text=questions[current_question])
  26.  
  27. # Function to check the answer and update the score
  28. def check_answer():
  29.     global current_question, score
  30.  
  31.     answer = answer_entry.get().strip()
  32.     correct_answer = answers[current_question]
  33.  
  34.     if answer.lower() == correct_answer.lower():
  35.         score += 1
  36.  
  37.     answer_entry.delete(0, tk.END)
  38.  
  39.     # Move to the next question or end the quiz
  40.     if current_question < len(questions) - 1:
  41.         current_question += 1
  42.         display_question()
  43.     else:
  44.         end_quiz()
  45.  
  46. # Function to end the quiz and display the final score
  47. def end_quiz():
  48.     question_label.config(text="Quiz ended. Your score: {}/{}".format(score, len(questions)))
  49.     answer_entry.config(state=tk.DISABLED)
  50.     check_button.config(state=tk.DISABLED)
  51.  
  52. # Create a label to display the question
  53. question_label = tk.Label(window, text="")
  54. question_label.pack(pady=10)
  55.  
  56. # Create an entry widget for entering the answer
  57. answer_entry = tk.Entry(window, width=30)
  58. answer_entry.pack(pady=5)
  59.  
  60. # Create a button to check the answer
  61. check_button = tk.Button(window, text="Check", command=check_answer)
  62. check_button.pack(pady=5)
  63.  
  64. # Display the first question
  65. display_question()
  66.  
  67. window.mainloop()
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement