Error after closing customtkinter window with Matplotlib figure

Question:

I am using matplotlib figure to show equations in a customtkinter gui program.
After closing the window, this error shows up and the program keeps running:

invalid command name "2444841565824check_dpi_scaling"
    while executing
"2444841565824check_dpi_scaling"
    ("after" script)
invalid command name "2444841677504update"
    while executing
"2444841677504update"
    ("after" script)

A sample of my code:

from tkinter import *
from customtkinter import *
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
matplotlib.use('TkAgg')

plt.rcParams["figure.facecolor"] = "2b2b2b"
plt.rcParams["axes.facecolor"] = "2b2b2b"
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = ['Times New Roman'] + plt.rcParams['font.serif']
plt.rcParams['mathtext.default'] = 'regular'

root = CTk()
root.geometry('1000x600+200+40')
root.minsize(width=800, height=600)
root.title('Something')
root.state("zoomed")

EquationFrame = CTkFrame(root, height=800,width=500)
EquationFrame.place(relx=0.239 , rely=0.03 , relwidth=0.4*1.8 , relheight=(0.3999*3)- 0.2688)

EquationOutput = Label(EquationFrame)
EquationOutput.place(relx=0.024, rely=0.3004,)

figure = plt.figure(figsize=(13.25, 2))
ax = figure.add_subplot(111)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

canvas = FigureCanvasTkAgg(figure, master=EquationOutput)
canvas.get_tk_widget().pack(side="top", fill="both", expand=True)
canvas._tkcanvas.pack(fill="both", expand=True, side="top")

ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set_visible(False)

ax.clear()
ax.text(-0.15, 0.37, f"$(something)^2 * (something)^3$", fontsize=50, fontdict=None).set_color("white")

root.mainloop()

How can I solve this?
Will it affect the program after building (using auto-py-to-exe)?

Answers:

The solution is simply adding this line:
root.protocol("WM_DELETE_WINDOW", root.quit)