If I define a Button click handler inside a function, it doesn't work. In the following example figures f1 and f2 look the same, but only if I press the button on f2, it produces output.
from matplotlib import pyplot as plt
from matplotlib.widgets import Button
def handler(*args, **kwargs):
print('handled')
def testfn():
f1 = plt.figure('f1')
b1 = Button(f1.add_axes([0.4, 0.3, 0.1, 0.04]), 'Click!')
b1.on_clicked(handler)
f2 = plt.figure('f2')
b2 = Button(f2.add_axes([0.4, 0.3, 0.1, 0.04]), 'Click!')
b2.on_clicked(handler)
testfn()
plt.show()