Skip to content

[Bug]: Event loop behavior differs across backends #31858

@iccir

Description

@iccir

Bug summary

I'm working on fixing several bugs with the macOS backend. I am trying to understand how show(), start_event_loop(), and stop_event_loop() should interact with each other.

I had several questions that I couldn't find in the documentation:

  1. If show(block=True) is called, should stop_event_loop() make it return?
  2. If start_event_loop() is called on a figure, should closing all windows make it return?
  3. What should be the behavior if start_event_loop() or show(block=True) is called when an event loop is already running from show() or another start_event_loop()?
  4. If stop_event_loop() is called without an event loop running, it appears to be a no-op across all backends. Should it instead throw an exception?

I wrote the attached code to test these. However, I am seeing different results from different backends. See charts in the "Actual Results" section.

Code for reproduction

import matplotlib.pyplot as plt
import matplotlib
import sys

example_index = int(sys.argv[1])

backend, mode = (
    ( "QtAgg",  "show" ),
    ( "TkAgg",  "show" ),
    ( "macosx", "show" ),
    ( "QtAgg",  "start_event_loop" ),
    ( "TkAgg",  "start_event_loop" ),
    ( "macosx", "start_event_loop" ),
)[ example_index ]

print(f"backend = '{backend}', mode = '{mode}'")


matplotlib.use(backend)

fig, ax = plt.subplots()
ax.set_title("Click anywhere to continue")
ax.plot([1, 2, 3])

clicked = []

def on_click(event):
    clicked.append((event.xdata, event.ydata))
    print("on_click")
    fig.canvas.stop_event_loop()

fig.canvas.mpl_connect('button_press_event', on_click)

fig, ax = plt.subplots()

if (mode == "show"):
    plt.show()
else:
    plt.show(block=False)
    fig.canvas.start_event_loop()

if (len(clicked) > 0):
    print(f"User clicked at: {clicked[0]}")
else:
    print(f"Event loop stopped without click")

Actual outcome

Does closing both windows stop the event loop?

backend mode Result
QtAgg show Yes
TkAgg show Yes
macosx show Yes
QtAgg start_event_loop No
TkAgg start_event_loop Yes
macosx start_event_loop No

Does clicking on Figure 1 stop the event loop?

backend mode Result
QtAgg show No
TkAgg show Yes
macosx show No
QtAgg start_event_loop Yes
TkAgg start_event_loop Yes
macosx start_event_loop Yes

Expected outcome

All backends should operate with the same behavior.

Additional information

No response

Operating system

No response

Matplotlib Version

Dev

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions