Unexpected control flow in this Python code with Matplotlib

This Python 3.12 code on Ubuntu 22.04 behaves as expected, except when I press q or ESC key to quit. Here is the code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import numpy as np, matplotlib.pyplot as plt
from pathlib import Path
from collections import namedtuple
from skimage.io import imread
from matplotlib.widgets import Slider
def update(u):
global minR, minG, minB
minR = sliderR.val
minG = sliderG.val
minB = sliderB.val
axImg.imshow(img)
plt.title(f'iFile {iFile}/{len(files)-1}')
plt.show() #----------------------------------------- step 3
def onpress(event):
global files, iFile, iStart, iEnd, img
match event.key:
case 'q' | 'escape':
plt.close() #------------------------------------- step 1
return #------------------------------------------ step 2
case 'pageup' | 'pagedown' as incr:
if incr == 'pagedown':
iFile = min(iFile+1, len(files)-1)
else:
iFile = max(iFile-1, 0)
case 'o':
path = Path('/home/paul/downloads/fig-0.png')
files = [f for f in Path(path.parent).glob('*.png')]
files.sort()
iFile = files.index(path)
iStart, iEnd = 0, np.inf
img = imread(files[iFile])
update(None)
nRows, nCols = 20, 15
fig = plt.figure(figsize=(12, 14))
axImg = plt.subplot2grid((nRows, nCols), (0, 0), rowspan=nRows-4, colspan=nCols)
axR = plt.subplot2grid((nRows, nCols), (nRows-3, 1), colspan=nCols-2)
axG = plt.subplot2grid((nRows, nCols), (nRows-2, 1), colspan=nCols-2)
axB = plt.subplot2grid((nRows, nCols), (nRows-1, 1), colspan=nCols-2)
sliderR = Slider(ax=axR, label='R', valmin=0, valmax=255, valinit=128, valstep=4)
sliderG = Slider(ax=axG, label='G', valmin=0, valmax=255, valinit=128, valstep=4)
sliderB = Slider(ax=axB, label='B', valmin=0, valmax=255, valinit=128, valstep=4)
sliderR.on_changed(update)
sliderG.on_changed(update)
sliderB.on_changed(update)
fig.canvas.mpl_connect('key_press_event', onpress)
plt.get_current_fig_manager().full_screen_toggle()
onpress(namedtuple('T', ['key'])('o')) #------------------- step 4
</code>
<code>import numpy as np, matplotlib.pyplot as plt from pathlib import Path from collections import namedtuple from skimage.io import imread from matplotlib.widgets import Slider def update(u): global minR, minG, minB minR = sliderR.val minG = sliderG.val minB = sliderB.val axImg.imshow(img) plt.title(f'iFile {iFile}/{len(files)-1}') plt.show() #----------------------------------------- step 3 def onpress(event): global files, iFile, iStart, iEnd, img match event.key: case 'q' | 'escape': plt.close() #------------------------------------- step 1 return #------------------------------------------ step 2 case 'pageup' | 'pagedown' as incr: if incr == 'pagedown': iFile = min(iFile+1, len(files)-1) else: iFile = max(iFile-1, 0) case 'o': path = Path('/home/paul/downloads/fig-0.png') files = [f for f in Path(path.parent).glob('*.png')] files.sort() iFile = files.index(path) iStart, iEnd = 0, np.inf img = imread(files[iFile]) update(None) nRows, nCols = 20, 15 fig = plt.figure(figsize=(12, 14)) axImg = plt.subplot2grid((nRows, nCols), (0, 0), rowspan=nRows-4, colspan=nCols) axR = plt.subplot2grid((nRows, nCols), (nRows-3, 1), colspan=nCols-2) axG = plt.subplot2grid((nRows, nCols), (nRows-2, 1), colspan=nCols-2) axB = plt.subplot2grid((nRows, nCols), (nRows-1, 1), colspan=nCols-2) sliderR = Slider(ax=axR, label='R', valmin=0, valmax=255, valinit=128, valstep=4) sliderG = Slider(ax=axG, label='G', valmin=0, valmax=255, valinit=128, valstep=4) sliderB = Slider(ax=axB, label='B', valmin=0, valmax=255, valinit=128, valstep=4) sliderR.on_changed(update) sliderG.on_changed(update) sliderB.on_changed(update) fig.canvas.mpl_connect('key_press_event', onpress) plt.get_current_fig_manager().full_screen_toggle() onpress(namedtuple('T', ['key'])('o')) #------------------- step 4 </code>
import numpy as np, matplotlib.pyplot as plt
from pathlib import Path
from collections import namedtuple
from skimage.io import imread
from matplotlib.widgets import Slider


def update(u):
  global minR, minG, minB

  minR = sliderR.val
  minG = sliderG.val
  minB = sliderB.val

  axImg.imshow(img)
  plt.title(f'iFile {iFile}/{len(files)-1}')
  plt.show() #----------------------------------------- step 3


def onpress(event):
  global files, iFile, iStart, iEnd, img

  match event.key:
    case 'q' | 'escape':
      plt.close() #------------------------------------- step 1
      return #------------------------------------------ step 2
    case 'pageup' | 'pagedown' as incr:
      if incr == 'pagedown':
        iFile = min(iFile+1, len(files)-1)
      else:
        iFile = max(iFile-1, 0)
    case 'o':
      path = Path('/home/paul/downloads/fig-0.png')
      files = [f for f in Path(path.parent).glob('*.png')]
      files.sort()
      iFile = files.index(path)
      iStart, iEnd = 0, np.inf

  img = imread(files[iFile])
  update(None)


nRows, nCols = 20, 15
fig = plt.figure(figsize=(12, 14))
axImg = plt.subplot2grid((nRows, nCols), (0, 0), rowspan=nRows-4, colspan=nCols)
axR = plt.subplot2grid((nRows, nCols), (nRows-3, 1), colspan=nCols-2)
axG = plt.subplot2grid((nRows, nCols), (nRows-2, 1), colspan=nCols-2)
axB = plt.subplot2grid((nRows, nCols), (nRows-1, 1), colspan=nCols-2)
sliderR = Slider(ax=axR, label='R', valmin=0, valmax=255, valinit=128, valstep=4) 
sliderG = Slider(ax=axG, label='G', valmin=0, valmax=255, valinit=128, valstep=4) 
sliderB = Slider(ax=axB, label='B', valmin=0, valmax=255, valinit=128, valstep=4) 
sliderR.on_changed(update)
sliderG.on_changed(update)
sliderB.on_changed(update)
fig.canvas.mpl_connect('key_press_event', onpress)
plt.get_current_fig_manager().full_screen_toggle()
onpress(namedtuple('T', ['key'])('o')) #------------------- step 4

When I set a breakpoint at step 1, start debugging with VS Code and press q or ESC key, the control goes to step 1, step 2 and then, unexpectedly, to step 3 and step 4. I expected 1, 2 and 4 sequence.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật