Программу виконану в консолі зробити з графічним інтерфейсом(tkinter)
Программу python виконану в консолі зробити з простим графічним інтерфейсом(tkinter)(import random
import time
def read_questions(filename):
questions = []
with open(filename, 'r') as f:
lines = f.readlines()
for i in range(0, len(lines), 6):
question = {}
question['question'] = lines.strip()
question['options'] = [lines[i+1].strip(), lines[i+2].strip(), lines[i+3].strip(), lines[i+4].strip()]
question['answer'] = lines[i+5].strip()
questions.append(question)
return questions
def take_test(questions):
random.shuffle(questions)
score = 0
start_time = time.time()
for i, question in enumerate(questions):
print(f"Питання {i+1}: {question['question']}")
for j, option in enumerate(question['options']):
print(f"{j+1}. {option}")
user_answer = input("Ваша відповідь: ")
if user_answer == question['answer']:
print("Правильно!")
score += 1
else:
print(f"Неправильно! Правильна відповідь - {question['answer']}")
end_time = time.time()
time_taken = round(end_time - start_time, 2)
print(f"Ваш результат - {score}/{len(questions)}.")
print(f"Час виконання - {time_taken} секунд.")
return score, time_taken
def save_results(score, time_taken, filename):
name = input("Будь ласка, введіть своє ім'я: ")
with open(filename, 'a') as f:
f.write(f"{name}: Результат - {score}/{len(questions)}, Час виконання - {time_taken} секунд.\n")
def show_answers(questions):
for i, question in enumerate(questions):
print(f"Питання {i+1}: {question['question']}")
for j, option in enumerate(question['options']):
print(f"{j+1}. {option}")
print(f"Правильна відповідь: {question['answer']}")
if __name__ == '__main__':
print("Ласкаво просимо до програми тестування знань!")
print("Будь ласка, оберіть тему:")
print("1. Історія")
print("2. Наука")
topic = input("Введіть свій вибір (1 або 2): ")
if topic == '1':
questions = read_questions('history_questions.txt')
elif topic == '2':
questions = read_questions('science_questions.txt')
else:
print("Невірний вибір.")
exit()
score, time_taken = take_test(questions)
choice = input("Чи хочете ви побачити правильні відповіді? (так / ні): ")
if choice == 'так':
show_answers(questions)
save_results(score, time_taken, 'results.txt')
print("Результати збережено у файл results.txt.")