好吧,这比我想象的要容易!对于那些感兴趣的人,我找到了使用figure.canvas.start_event_loop()
and的解决方案figure.canvas.stop_event_loop()
。
这是一个简单的例子:
from matplotlib import pyplot as plt
class FigEventLoopDemo(object):
def __init__(self):
self.fig, self.ax = plt.subplots(1, 1, num='Event loop demo')
self.clickme = self.ax.text(0.5, 0.5, 'click me',
ha='center', va='center',
color='r', fontsize=20, picker=10)
# add a callback that triggers when the text is clicked
self.cid = self.fig.canvas.mpl_connect('pick_event', self.on_pick)
# start a blocking event loop
print("entering a blocking loop")
self.fig.canvas.start_event_loop(timeout=-1)
def on_pick(self, event):
if event.artist is self.clickme:
# exit the blocking event loop
self.fig.canvas.stop_event_loop()
print("now we're unblocked")