Я реализовал функцию выбора ROI, которая порождает окно matplolib.Как только выбор сделан, я хочу закрыть эту функцию и продолжить с остальным моим кодом.В конце выбора остальная часть кода работает нормально, но окно никогда не закрывается.
Я пытался закрыть окно тремя способами:
plt.close('all')
self.fm = plt.get_current_fig_manager()
self.fm.destroy()
plt.show(block=False)
но ни один из них не закрывает окно.
Вот рабочий пример класса селектора:
class RectangleSelection(object):
def __init__(self, img):
self.rectangle = None
self.img = img
self.done = False
#Setup the figure
self.fig, self.ax = plt.subplots()
self.fm = plt.get_current_fig_manager()
plt.ion
plt.imshow(self.img, cmap='gray')
self.RS = RectangleSelector(self.ax, self.onselect,
drawtype='box', useblit=True,
button=[1, 3],
minspanx=5, minspany=5,
spancoords='pixels',
interactive=True)
plt.connect('key_press_event', self.toggle_selector)
plt.show()
def onselect(self, e_click, e_release):
# [...] do stuff
def toggle_selector(self, event):
if event.key in ['Q', 'q'] and self.RS.active:
self.RS.set_active(False)
self.done = True
def close(self):
logging.debug("Closing selection window")
plt.show(block=False)
plt.close('all')
self.fm.destroy()
и вызывающей функции
selector = RectangleSelection(img)
while not selector.done:
pass
crop_box = selector.rectangle
logging.debug("Got crop_box %s", str(crop_box))
selector.close() # I want the window to close at this point
plt.pause(0.5)
long_process(crop_box)
# The window eventually closes when the process completely finishes.
selector.close()
вызывается (как показано в сообщении регистрации), но это не приводит к закрытию окна.
Более короткий рабочий пример в приглашении ipython следующий:
import matplotlib.pyplot as plt
plt.ion()
plt.plot([1.6, 2.7]) # This shows the plot in a window
plt.title("Test Title") # This works showing the link between code and plot is there
plt.clf() # This works too, clearing the figure
plt.close() # This makes the plot window unresponsive but does not close it.