I have made a python program that creates a tkinter window, with multiple tabs. Each tab shows a different matplotlib figure.
My problem is that when I press the x button in the top right corner to close the window, the window appears to close but I cannot enter a new command into the terminal, indicating that the program is still running. I have to close the terminal to fully stop the program. How do I fix this?
This is a short version of my code:
import numpy as np
import h5py as h5
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import tkinter as tk
from tkinter import ttk
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
window = tk.Tk()
window.title("Plotting data in the hdf5 file")
mytabs = ttk.Notebook(window)
tab1 = ttk.Frame(mytabs)
tab2 = ttk.Frame(mytabs)
tab3 = ttk.Frame(mytabs)
tab4 = ttk.Frame(mytabs)
mytabs.add(tab1, text = "Velocity")
mytabs.add(tab2, text = "Momentum Flux")
mytabs.add(tab3, text = "Pressure and forcing")
mytabs.add(tab4, text = "Diagram")
mytabs.pack(expand = 1, fill ="both")
## code to read some data here
## code to make 4 different figures here
## fig, ax = plt.subplots(2,2, figsize = (12,12))
## ... etc.
def plot(fig,tab):
canvas = FigureCanvasTkAgg(fig, master = tab)
canvas.draw()
canvas.get_tk_widget().pack()
toolbar = NavigationToolbar2Tk(canvas, tab)
toolbar.update()
canvas.get_tk_widget().pack()
plot(fig,tab1)
plot(fig2,tab2)
plot(fig3,tab3)
plot(fig4,tab4)
window.mainloop()
I am on Ubuntu 22.04.4 LTS if it matters.
def on_closing():
window.quit()
window.destroy()
window.protocol("WM_DELETE_WINDOW", on_closing)
It's not clear why this is happening, but try this function called it just before you call the main loop ( so at the end, just before the last line)
This function window.quit()
basically stops the main loop.
window.destroy()
makes sure that the window closed. And then just pass it as a protocol for WM_DELETE_WINDOW