Напиши код графического интерфейса на языке python для следующего кода…
- User
Напиши код графического интерфейса на языке python для следующего кода questionsandanswers = {"Вопрос первый": [ ["вариант1", "вариант2", "вариант3"], 1], "Вопрос второй": [ ["вариант1", "вариант2", "вариант3"], 2], "Вопрос третий": [ ["вариант1", "вариант2", "вариант3"], 3] } total = 0
for question, answer in questionsandanswers.items(): print(question) print(answer[0]) choice = (int(input("Введите номер варианта: "))) if choice == answer[1]: total += 1
print(f"Ваше число баллов {total}")
- Assistant
Вот пример кода графического интерфейса на языке Python, который использует библиотеку tkinter:
import tkinter as tk questionsandanswers = { "Вопрос первый": [["вариант1", "вариант2", "вариант3"], 1], "Вопрос второй": [["вариант1", "вариант2", "вариант3"], 2], "Вопрос третий": [["вариант1", "вариант2", "вариант3"], 3] } total = 0 def check_answer(question, answer, choice): global total if choice == answer: total += 1 def next_question(): global question_index if question_index < len(questionsandanswers.keys()): question = list(questionsandanswers.keys())[question_index] answer = questionsandanswers[question][1] choices = questionsandanswers[question][0] question_label.config(text=question) for i, choice in enumerate(choices): choice_buttons[i].config(text=choice) question_index += 1 else: end_quiz() def end_quiz(): result_label.config(text=f"Ваше число баллов {total}") for button in choice_buttons: button.config(state=tk.DISABLED) next_button.config(text="Завершено", state=tk.DISABLED) question_index = 0 window = tk.Tk() window.title("Тест") window.geometry("400x200") question_label = tk.Label(window, text="") question_label.pack() choice_buttons = [] for _ in range(3): button = tk.Button(window, text="", command=lambda: next_question()) choice_buttons.append(button) button.pack() result_label = tk.Label(window, text="") result_label.pack() next_button = tk.Button(window, text="Далее", command=lambda: next_question()) next_button.pack() next_question() window.mainloop() - User
Напиши код графического интерфейса на языке python для следующего кода questionsandanswers = {“Вопрос первый”: [ [“вариант1”, “вариант2”, “вариант3”, "вариант4"], 1], “Вопрос второй”: [ [“вариант1”, “вариант2”, “вариант3” "вариант4"], 2], “Вопрос третий”: [ [“вариант1”, “вариант2”, “вариант3”, "вариант4"], 3] } total = 0
for question, answer in questionsandanswers.items(): print(question) print(answer[0]) choice = (int(input("Введите номер варианта: “))) if choice == answer[1]: total += 1
print(f"Ваше число баллов {total}”)
- Assistant
Вот пример кода графического интерфейса на языке Python с использованием библиотеки tkinter для вашего задания:
import tkinter as tk questionsandanswers = { "Вопрос первый": [["вариант1", "вариант2", "вариант3", "вариант4"], 1], "Вопрос второй": [["вариант1", "вариант2", "вариант3", "вариант4"], 2], "Вопрос третий": [["вариант1", "вариант2", "вариант3", "вариант4"], 3] } total = 0 question_index = 0 def check_answer(choice): global total, question_index question = list(questionsandanswers.keys())[question_index] answer = questionsandanswers[question][1] if choice == answer: total += 1 question_index += 1 if question_index >= len(questionsandanswers): end_quiz() else: show_question() def show_question(): question = list(questionsandanswers.keys())[question_index] choices = questionsandanswers[question][0] question_label.config(text=question) for i, choice in enumerate(choices): choice_buttons[i].config(text=choice) def end_quiz(): question_label.config(text=f"Ваше число баллов {total}") for button in choice_buttons: button.config(state=tk.DISABLED) window = tk.Tk() window.title("Тест") window.geometry("400x200") question_label = tk.Label(window, text="") question_label.pack() choice_buttons = [] for _ in range(4): button = tk.Button(window, text="", command=lambda i=_: check_answer(i+1)) choice_buttons.append(button) button.pack() show_question() window.mainloop()