Matplotlib Figure Does Not Continue Program Flow After Close Event Triggered Inside Tk App
I've come across a really annoying difference between how windows and mac handles a python tk window and matplotlib figure close_event. My problem is thus, I am trying to load a
Solution 1:
You can use plt.ion()
to turn on Matplotlib's interactive mode, but this by itself will cause the program to continue without blocking the flow. To manually block the flow, use self.fig.canvas.start_event_loop_default()
and self.fig.canvas.stop_event_loop()
to pause the program flow until events are captured.
Implemented in your minimal example:
from Tkinter import *
from matplotlib import pyplot as plt
class Plotter:
def __init__(self):
plt.ion()
self.fig = plt.figure()
self.fig.canvas.mpl_connect('close_event', self.dispose)
self.fig.canvas.mpl_connect('button_press_event', self.on_mouse_click)
plt.plot(1, 2, 'r*')
plt.show()
self.fig.canvas.start_event_loop_default()
print "done with plotter"
def dispose(self, event):
self.fig.canvas.stop_event_loop()
print "disposed"
def on_mouse_click(self, event):
print 'mouse clicked!'
if __name__ == '__main__':
def pressed():
print 'button pressed'
Plotter()
print 'YAY'
root = Tk()
button = Button(root, text='Press', command=pressed)
button.pack(pady=20, padx=20)
root.mainloop()
Post a Comment for "Matplotlib Figure Does Not Continue Program Flow After Close Event Triggered Inside Tk App"