简体   繁体   中英

tkinter GUI does not terminate with 'X' when using matplotlib.pyplot

I am running a simple tkinter application that plots some data with an animated graph. When using matplotlib.figure.Figure and add_subplot , when I close the window with the upper-menu 'X' button (windows default) the program terminates well. Working code:

self.f = matplotlib.figure.Figure(figsize=(6, 4), dpi=100)
self.ax = self.f.add_subplot(111)

Now, when I try to use matplotlib.pyplot.figure together with matplotlib.pyplot.subplot2grid , then the 'X' does close the window but the program keeps running.

self.f = plt.figure(figsize=(6, 4), dpi=100)
self.ax = plt.subplot2grid((6,4), (0,0), rowspan=6, colspan=4)

I've tried adding matplotlib.pyplot.close("all") at the end of the program but the app doesn't exit the mainloop at all:

app = myApp()
app.mainloop() # doesn't exit
plt.close("all") # doesn't get executed

Any possible reason and alternatives?

PS: Using self.protocol("WM_DELETE_WINDOW", self.destroy) doesn't work (where self is the tk.TK instance).

You can try handling the WM_DELETE_WINDOW event and close all plots like this:

import matplotlib.pyplot as plt

def on_closing():
   plt.close("all")
   app.destroy()

app = myApp()
.
.
.
app.protocol("WM_DELETE_WINDOW", on_closing)
app.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM