簡體   English   中英

使用 matplotlib.pyplot 時,tkinter GUI 不會以“X”終止

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

我正在運行一個簡單的 tkinter 應用程序,它用動畫圖繪制一些數據。 當使用matplotlib.figure.Figureadd_subplot ,當我使用上方菜單“X”按鈕(Windows 默認)關閉窗口時,程序會很好地終止。 工作代碼:

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

現在,當我嘗試使用matplotlib.pyplot.figure連同matplotlib.pyplot.subplot2grid ,那么“X”確實關閉了窗口,但該程序繼續運行。

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

我嘗試在程序末尾添加matplotlib.pyplot.close("all")但該應用程序根本沒有退出主mainloop

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

任何可能的原因和替代方案?

PS:使用self.protocol("WM_DELETE_WINDOW", self.destroy)不起作用(其中 self 是tk.TK實例)。

您可以嘗試處理WM_DELETE_WINDOW事件並像這樣關閉所有圖:

import matplotlib.pyplot as plt

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM