Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Z2h-A6n opened this issue Jun 11, 2024 · 4 comments · Fixed by #28269
Closed

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

Z2h-A6n opened this issue Jun 11, 2024 · 4 comments · Fixed by #28269
Milestone

Comments

@Z2h-A6n
Copy link

Z2h-A6n commented Jun 11, 2024

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

@tacaswell
Copy link
Member

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
Copy link
Author

Z2h-A6n commented 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 main and tk.window_focus: True works fine.

Should I close this issue?

@tacaswell tacaswell modified the milestone: v3.9.1 Jun 11, 2024
@tacaswell tacaswell linked a pull request Jun 11, 2024 that will close this issue
5 tasks
@tacaswell
Copy link
Member

Fixed by #28269 and the fix will be release in 3.9.1

@tacaswell
Copy link
Member

Thank you for reporting this @Z2h-A6n !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants