The following code puts some text on the screen, and makes a callback that should replace the text with “Please wait…” and then sleep for 2 seconds when the user clicks on the figure. Instead, when the user clicks on the figure, the code waits for 2 seconds and then updates the screen.
Is this a bug, or an error on my part? I was sure to create the new text and call plt.draw()
before sleep(2)
so I would expect the new text to appear before the 2-second sleep.
import matplotlib.pyplot as plt
from time import sleep
class TimeDemo():
def __init__(self):
self.fig, self.ax = plt.subplots()
self.ax.axis("off")
self.axtext = self.ax.annotate("Please click somewhere.", xy=(0, 0.5), ha="left", va="center")
self.donecid = self.fig.canvas.mpl_connect("button_press_event", self.donecallback)
def sleeper(self):
sleep(2)
plt.draw()
def donecallback(self, event):
self.fig.canvas.mpl_disconnect(self.donecid)
self.axtext.remove()
self.axtext = self.ax.annotate("Please wait...", xy=(0, 0.5), ha="left", va="center")
plt.draw()
self.sleeper()
mydemo = TimeDemo()
plt.show()