Skip to content

Commit c79514a

Browse files
authored
Merge pull request #28270 from meeseeksmachine/auto-backport-of-pr-28269-on-v3.9.x
Backport PR #28269 on branch v3.9.x (Handle GetForegroundWindow() returning NULL.)
2 parents cb4f0d6 + 8cb17f8 commit c79514a

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

lib/matplotlib/backends/_backend_tk.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def _restore_foreground_window_at_end():
4444
try:
4545
yield
4646
finally:
47-
if mpl.rcParams['tk.window_focus']:
47+
if foreground and mpl.rcParams['tk.window_focus']:
4848
_c_internal_utils.Win32_SetForegroundWindow(foreground)
4949

5050

src/_c_internal_utils.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ static py::object
111111
mpl_GetForegroundWindow(void)
112112
{
113113
#ifdef _WIN32
114-
return py::capsule(GetForegroundWindow(), "HWND");
114+
if (HWND hwnd = GetForegroundWindow()) {
115+
return py::capsule(hwnd, "HWND");
116+
} else {
117+
return py::none();
118+
}
115119
#else
116120
return py::none();
117121
#endif

0 commit comments

Comments
 (0)