Skip to content

[Bug]: rcParam tk.window_focus: True is causes crash on Linux in version 3.9.0. #28374

Closed
@Z2h-A6n

Description

@Z2h-A6n

Bug summary

Setting tk.window_focus to True in matplotlibrc or matplotlib.rcParams causes a crash when plotting on a Linux system with matplotlib 3.9.0.

Code for reproduction

import matplotlib as mpl

# I removed my system-wide matplotlibrc file for this test, but the same
# behavior is produced when the following are set in matplotlibrc instead.
mpl.use('TkAgg')
mpl.rcParams['tk.window_focus'] = True

import matplotlib.pyplot as plt

plt.plot([1, 2, 3])
plt.show()

Actual outcome

No plot is produced, and a TypeError is thrown with the following traceback:

Traceback (most recent call last):
  File "/home/zach/temp/mpl-test/test.py", line 10, in <module>
    plt.plot([1, 2, 3])
  File "/home/zach/.local/share/venvs/c1078f6cd069ee2916e64fe0fd9fd7f2/venv/lib/python3.12/site-packages/matplotlib/pyplot.py", line 3708, in plot
    return gca().plot(
           ^^^^^
  File "/home/zach/.local/share/venvs/c1078f6cd069ee2916e64fe0fd9fd7f2/venv/lib/python3.12/site-packages/matplotlib/pyplot.py", line 2656, in gca
    return gcf().gca()
           ^^^^^
  File "/home/zach/.local/share/venvs/c1078f6cd069ee2916e64fe0fd9fd7f2/venv/lib/python3.12/site-packages/matplotlib/pyplot.py", line 1088, in gcf
    return figure()
           ^^^^^^^^
  File "/home/zach/.local/share/venvs/c1078f6cd069ee2916e64fe0fd9fd7f2/venv/lib/python3.12/site-packages/matplotlib/pyplot.py", line 1022, in figure
    manager = new_figure_manager(
              ^^^^^^^^^^^^^^^^^^^
  File "/home/zach/.local/share/venvs/c1078f6cd069ee2916e64fe0fd9fd7f2/venv/lib/python3.12/site-packages/matplotlib/pyplot.py", line 545, in new_figure_manager
    return _get_backend_mod().new_figure_manager(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zach/.local/share/venvs/c1078f6cd069ee2916e64fe0fd9fd7f2/venv/lib/python3.12/site-packages/matplotlib/backend_bases.py", line 3521, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zach/.local/share/venvs/c1078f6cd069ee2916e64fe0fd9fd7f2/venv/lib/python3.12/site-packages/matplotlib/backend_bases.py", line 3526, in new_figure_manager_given_figure
    return cls.FigureCanvas.new_manager(figure, num)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zach/.local/share/venvs/c1078f6cd069ee2916e64fe0fd9fd7f2/venv/lib/python3.12/site-packages/matplotlib/backend_bases.py", line 1811, in new_manager
    return cls.manager_class.create_with_canvas(cls, figure, num)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zach/.local/share/venvs/c1078f6cd069ee2916e64fe0fd9fd7f2/venv/lib/python3.12/site-packages/matplotlib/backends/_backend_tk.py", line 479, in create_with_canvas
    with _restore_foreground_window_at_end():
  File "/usr/lib/python3.12/contextlib.py", line 144, in __exit__
    next(self.gen)
  File "/home/zach/.local/share/venvs/c1078f6cd069ee2916e64fe0fd9fd7f2/venv/lib/python3.12/site-packages/matplotlib/backends/_backend_tk.py", line 48, in _restore_foreground_window_at_end
    _c_internal_utils.Win32_SetForegroundWindow(foreground)
TypeError: Win32_SetForegroundWindow(): incompatible function arguments. The following argument types are supported:
    1. (hwnd: capsule) -> None

Invoked with: None

Expected outcome

The following figure is produced with the same code using matplotlib 3.8.4:

Figure_1

Additional information

The bug occurs in Linux with matplotlib 3.9.0, when tk.window_focus is True in matplotlibrc or matplotlib.rcParams. The appropriate plot is produced if either of these conditions is changed, i.e. both of the following produce the correct plot:

  • matplotlib 3.8.4 with tk.window_focus: True
  • matplotlib 3.9.0 with tk.window_focus: False

I think the bug is related to 0097b02, though I'm not quite sure how to fix it.

Operating system

Arch Linux

Matplotlib Version

3.9.0

Matplotlib Backend

tkagg

Python version

Python 3.12.3

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions