Repository navigation
Closed
Milestone
Description
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:
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
Labels
No labels

Activity
tacaswell commentedon Jun 11, 2024
I can reproduce this in 3.9.0, but it is working on main or v3.9.x
I suspect that this was fixed by #28269
Z2h-A6n commentedon Jun 11, 2024
You're right, I got the commit history a bit mixed up and thought #28269 was incorporated in 3.9.0, and might have caused the bug. I just tested on
mainandtk.window_focus: Trueworks fine.Should I close this issue?
tacaswell commentedon Jun 11, 2024
Fixed by #28269 and the fix will be release in 3.9.1
tacaswell commentedon Jun 11, 2024
Thank you for reporting this @Z2h-A6n !