Q:

Can't quit out of matplotlib window

I have this code that's supposed to draw a box in a matplotlib window, then print the coordinates of that image, I also have it so that when I click 'q' it'll move on to the next image, however this doesn't work. But I can click the x button on the window, and it'll move on to the next image, but not print the coordinates
import os
import matplotlib.pyplot as plt
import cv2
from matplotlib.widgets import RectangleSelector

img = None
tl_list = []
br_list = []
object_list = []

image_folder = 'images'
savedir = 'annotations'
obj = 'thumbs up'

def line_select_callback(clk, rls):
    global tl_list
    global br_list
    tl_list.append((int(clk.xdata), int(clk.ydata)))
    br_list.append((int(rls.xdata), int(rls.ydata)))

def toggle_selector(event):
    toggle_selector.RS.set_active(True)

def onkeypress(event):
    global tl_list
    global br_list
    global object_list
    global img
    if event.key == 'q':
        print(tl_list, br_list)
        tl_list = []
        br_list = []
        img = None
        plt.close()

if __name__ == '__main__':
    for n, image_file in enumerate(os.scandir(image_folder)):
        img = image_filewwwwwetgfdsx
        fig, ax = plt.subplots(1)
        image = cv2.imread(image_file.path)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        ax.imshow(image)

        toggle_selector.RS = RectangleSelector(
            ax, line_select_callback,
            drawtype='box', useblit =True,
            button=[1], # left mouse click
            minspanx=5, minspany=5, spancoords='pixels',
            interactive=True
        )
        bbox = plt.connect('key_press_event', toggle_selector)
        key = plt.connect('key_press_event', onkeypress)
        plt.show()

A:

Sorry everyone, I just had to update Matplotlib, this code only works in 3.0, not in 2.0. 

