CustomTkinter icon indicating copy to clipboard operation
CustomTkinter copied to clipboard

Error after closing the window with Matplotlib figure

Open HussainiProgrammer opened this issue 2 years ago • 1 comments

I am using matplotlib figure to show equations. 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()

HussainiProgrammer avatar Dec 28 '22 16:12 HussainiProgrammer

I have the same problem and I have to kill the terminal to stop it.

iTerminatorX avatar Jan 21 '23 06:01 iTerminatorX

Quit mainloop before killing the window:

def Exit():
    root.quit()

root.protocol("WM_DELETE_WINDOW", Exit)

SaimLam avatar Jan 27 '23 07:01 SaimLam

I tried that multiple times, I tried both root.destroy()andsys.exit()`, but nothing changes. Maybe I will try again later...

HussainiProgrammer avatar Jan 27 '23 07:01 HussainiProgrammer

Thanks @SaimLam The solution is root.protocol("WM_DELETE_WINDOW", root.quit)

HussainiProgrammer avatar Feb 03 '23 17:02 HussainiProgrammer

Even with root.quit I got the error. For me it worked by adding plt.close()

def Exit():
    plt.close()
    root.quit()

root.protocol("WM_DELETE_WINDOW", Exit)

tobbergm avatar Oct 04 '23 13:10 tobbergm

that these things are not in the guide is another level of inability of the programmer. this project is so old and also dead.

Ezriral avatar Mar 20 '24 06:03 Ezriral