I have a very simple programme that displays a simple plot on press of a button. My problem is when I close the application window, the programme keeps running until I kill it from the terminal. Below is my code and my investigation showed the issue is caused by
matplotlib.use('TkAgg')
But I don't know how to fix it! If it helps, I'm running on OSX.
#!/usr/bin/python
from Tkinter import *
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
# ------ End of imports
class Ops:
def show_plot(self):
self.f, self.figarray = plt.subplots(1, sharex=True, sharey=True)
self.figarray.plot((1,2,3),(1,2,3))
plt.tight_layout()
self.canvas = FigureCanvasTkAgg(self.f, master=self.mainFrame)
self.canvas._tkcanvas.config(background='white', borderwidth=0, highlightthickness=0)
self.canvas._tkcanvas.pack(side=TOP, fill=BOTH)
class GUI(Ops):
def __init__(self, master):
self.master = master
self.width = self.master.winfo_screenwidth() # Width of the screen
self.height = self.master.winfo_screenheight() # Height of the screen
self.x = (self.width / 2)
self.y = (self.height / 2)
self.master.geometry("%dx%d+%d+%d" % (self.width, self.height, self.x, self.y))
self.mainFrame = Frame(self.master) # Generate the main container
self.mainFrame.pack()
# ---------- TOP FRAME ----------
self.topFrame = Frame(self.mainFrame)
self.topFrame.pack()
self.browse_button = Button(self.topFrame, text="Plot", command=self.show_plot)
self.browse_button.grid()
class App:
def __init__(self):
self.file_handler = Ops()
self.root = Tk()
self.gui_handler = GUI(self.root)
def run(self):
self.root.mainloop()
Application = App()
Application.run()
You need to call root.quit()
to end the Tk.mainloop(). For example, see the answer here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With