Tkinter+Python: Tkinter window crashing in a specific situation

This code is to create a simple graphical quiz. The tkinter window always seems to crash when i press the multiple choice button on the substraction quiz. It doesnt crash on any other type of quiz. Sometimes it works when i click the multiple choice button only to crash further into the quiz.
Heres the code:

import tkinter as tk
import random

def main_menu():
    clear_window()
    root.configure(bg='DodgerBlue4')

    title = tk.Label(root, text="Math Quiz", font=("Arial", 32), bg='DodgerBlue4', fg='white')
    title.pack(pady=40)

    button_frame = tk.Frame(root, bg='DodgerBlue4')
    button_frame.pack(pady=20)

    operations = [
        ("Multiplication", lambda: select_quiz_type('Multiplication'), 'red'),
        ("Addition", lambda: select_quiz_type('Addition'), 'forest green'),
        ("Subtraction", lambda: select_quiz_type('Subtraction'), 'blue'),
        ("Division", lambda: select_quiz_type('Division'), 'gold'),
    ]

    for operation, command1, color in operations:
        if command1:
            tk.Button(button_frame, text=operation, font=("Arial", 18), command=command1, bg=color, fg='white', width=12).pack(side='left', padx=20)


def clear_window():
    for widget in root.winfo_children():
        widget.destroy()

def select_quiz_type(operation):
    clear_window()
    root.configure(bg='DodgerBlue4')

    label = tk.Label(root, text=f"{operation} Quiz Type", font=("Arial", 24), bg='DodgerBlue4', fg='white')
    label.pack(pady=20)

    button_frame = tk.Frame(root, bg='DodgerBlue4')
    button_frame.pack(pady=20)

    tk.Button(button_frame, text="Multiple Choice", font=("Arial", 18), command=lambda: start_quiz(operation, "multiple_choice"), bg='gold', fg='black').pack(side='left', padx=20)
    tk.Button(button_frame, text="Submit Box", font=("Arial", 18), command=lambda: start_quiz(operation, "submit_box"), bg='gold', fg='black').pack(side='left', padx=20)

def start_quiz(operation, quiz_type):
    if operation == "Multiplication":
        create_quiz("Multiplication", lambda: (random.randint(1, 12), random.randint(1, 12)), lambda x, y: x * y, quiz_type)
    elif operation == "Addition":
        create_quiz("Addition", lambda: (random.randint(0, 100), random.randint(0, 100)), lambda x, y: x + y, quiz_type)
    elif operation == "Subtraction":
        create_quiz("Subtraction", lambda: (random.randint(0, 100), random.randint(0, 100)), lambda x, y: x - y, quiz_type)
    elif operation == "Division":
        create_quiz("Division", lambda: (random.randint(1, 100), random.randint(1, 10)), lambda x, y: x // y if y != 0 else 1, quiz_type)

def create_quiz(title, question_generator, answer_function, quiz_type):
    global current_operation, quiz_questions, quiz_answers, current_question, score, question_label, answer_buttons, answer_entry, current_quiz_type
    clear_window()

    current_operation = title.split()[0]
    current_quiz_type = quiz_type

    tk.Label(root, text=f"{title} Quiz", font=("Arial", 24), bg='DodgerBlue4', fg='white').pack(pady=20)

    quiz_questions = []
    quiz_answers = []

    for _ in range(10):
        x, y = question_generator()
        quiz_questions.append((x, y))
        quiz_answers.append(answer_function(x, y))

    current_question = 0
    score = 0

    question_label = tk.Label(root, font=("Arial", 18), bg='DodgerBlue4', fg='white')
    question_label.pack(pady=20)

    if quiz_type == "multiple_choice":
        answer_buttons = []
        for i in range(4):
            btn = tk.Button(root, font=("Arial", 18), bg='gold', fg='black', command=lambda control=i: check_mp_ans(control))
            btn.pack(pady=5)
            answer_buttons.append(btn)
    else:
        answer_entry = tk.Entry(root, font=("Arial", 18))
        answer_entry.pack(pady=10)
        tk.Button(root, text="Submit", font=("Arial", 18), command=check_sb_ans, bg='gold', fg='black').pack(pady=20)

    update_question()

def update_question():
    global current_question, quiz_questions, question_label, quiz_answers, current_quiz_type, answer_buttons
    if current_question < len(quiz_questions):
        x, y = quiz_questions[current_question]
        symbol = 'x' if current_operation == 'Multiplication' else '+' if current_operation == 'Addition' else '-' if current_operation == 'Subtraction' else '/'
        question_label.config(text=f"Question {current_question + 1}: {x} {symbol} {y} = ?")

        if current_quiz_type == "multiple_choice":
            correct_answer = quiz_answers[current_question]
            choices = [correct_answer]
            while len(choices) < 4:
                choice = random.randint(correct_answer - 10, correct_answer + 10)
                if choice not in choices and choice >= 0:
                    choices.append(choice)
            random.shuffle(choices)
            for i, choice in enumerate(choices):
                answer_buttons[i].config(text=str(choice))
    else:
        show_results()

def check_mp_ans(control):
    global current_question, score, quiz_answers
    selected_answer = int(answer_buttons[control].cget('text'))
    correct_answer = quiz_answers[current_question]
    if selected_answer == correct_answer:
        score += 1
        result_text = "Correct!"
    else:
        result_text = f"Wrong! The correct answer was {correct_answer}."

    current_question += 1
    show_answer_result(result_text)

def check_sb_ans():
    global current_question, score, quiz_answers, answer_entry
    try:
        user_answer = int(answer_entry.get())
    except ValueError:
        user_answer = None
    correct_answer = quiz_answers[current_question]
    if user_answer == correct_answer:
        score += 1
        result_text = "Correct!"
    else:
        result_text = f"Wrong! The correct answer was {correct_answer}."

    current_question += 1
    answer_entry.delete(0, tk.END)
    show_answer_result(result_text)

def show_answer_result(result_text):
    clear_window()
    root.configure(bg='DodgerBlue4')

    tk.Label(root, text=result_text, font=("Arial", 24), bg='DodgerBlue4', fg='white').pack(pady=20)
    tk.Button(root, text="Next", font=("Arial", 18), command=recreate_question_interface, bg='gold', fg='black').pack(pady=20)

def recreate_question_interface():
    clear_window()
    global question_label, answer_buttons, answer_entry
    question_label = tk.Label(root, font=("Arial", 18), bg='DodgerBlue4', fg='white')
    question_label.pack(pady=20)
    if current_quiz_type == "multiple_choice":
        answer_buttons = []
        for i in range(4):
            btn = tk.Button(root, font=("Arial", 18), bg='gold', fg='black', command=lambda control=i: check_mp_ans(control))
            btn.pack(pady=5)
            answer_buttons.append(btn)
    else:
        answer_entry = tk.Entry(root, font=("Arial", 18))
        answer_entry.pack(pady=10)
        tk.Button(root, text="Submit", font=("Arial", 18), command=check_sb_ans, bg='gold', fg='black').pack(pady=20)
    update_question()

def show_results():
    global score, quiz_questions
    clear_window()
    root.configure(bg='DodgerBlue4')

    tk.Label(root, text="Quiz Complete!", font=("Arial", 24), bg='DodgerBlue4', fg='white').pack(pady=20)
    tk.Label(root, text=f"Your score: {score}/{len(quiz_questions)}", font=("Arial", 18), bg='DodgerBlue4', fg='white').pack(pady=10)
    tk.Button(root, text="Main Menu", font=("Arial", 18), command=main_menu, bg='gold', fg='black').pack(pady=20)

root = tk.Tk()
root.geometry("900x600")
main_menu()
root.mainloop()

I tried it on two devices and it didnt work there too. The button grays out and the tkinter window stops responding. Its weird because its only for that specific multiple choice substraction quiz

New contributor

Anish Patil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật