import tkinter as tk from tkinter import messagebox class TestApp: def init(self,…
- User
import tkinter as tk from tkinter import messagebox
class TestApp: def init(self, master): self.master = master self.master.title("Тест по программированию") self.current_question = 0 self.score = 0
self.questions = { "Вопрос 1": {"answers": ["Ответ 1", "Ответ 2", "Ответ 3", "Ответ 4"], "correct": 0}, "Вопрос 2": {"answers": ["Ответ 1", "Ответ 2", "Ответ 3", "Ответ 4"], "correct": 1}, "Вопрос 3": {"answers": ["Ответ 1", "Ответ 2", "Ответ 3", "Ответ 4"], "correct": 2}, } self.create_widgets() def create_widgets(self): self.question_label = tk.Label(self.master, text="") self.question_label.pack(padx=15) self.radio_var = tk.IntVar() self.radio_buttons = [] for i in range(4): radio_button = tk.Radiobutton(self.master, text="", variable=self.radio_var, value=i) radio_button.pack(pady=15) self.radio_buttons.append(radio_button) self.submit_button = tk.Button(self.master, text="Ответить", command=self.check_answer) self.submit_button.pack(pady=15) self.display_question() def display_question(self): question = list(self.questions.keys())[self.current_question] self.question_label.config(text=question) answers = self.questions[question]["answers"] for i, radio_button in enumerate(self.radio_buttons): radio_button.config(text=answers[i]) def check_answer(self): selected_answer = self.radio_var.get() correct_answer = self.questions[list(self.questions.keys())[self.current_question]]["correct"] if selected_answer == correct_answer: self.score += 1 messagebox.showinfo("Result", "Correct!") else: messagebox.showinfo("Result", "Incorrect!") self.current_question += 1 if self.current_question == len(self.questions): messagebox.showinfo("Final Score", f"Your score: {self.score}/{len(self.questions)}") self.master.quit() else: self.display_question()if name == "main": root = tk.Tk() app = TestApp(root) root.mainloop()
- Assistant
The code defines a simple multiple-choice test application using tkinter. The application presents a series of questions with four possible answer choices. The user selects an answer and clicks the "Ответить" button to submit their response. A messagebox is then displayed indicating whether the response was correct or incorrect. The user's score is kept track of, and at the end of the test, a final score messagebox is displayed.
The test questions and answers are stored in a dictionary, and the display_question() method is used to update the question label and answer choices based on the current question index. The check_answer() method compares the selected answer with the correct answer and updates the score accordingly.
To run the application, create an instance of the TestApp class and start the tkinter mainloop.
- User
give me a correct code
- Assistant