Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Matplotlib Device Scaling on Windows#30599

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
melwyncarlo wants to merge2 commits intomatplotlib:main
base:main
Choose a base branch
Loading
frommelwyncarlo:issue_26733
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletionslib/matplotlib/backends/_backend_tk.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -602,6 +602,8 @@ def destroy(*args):
self.window.protocol("WM_DELETE_WINDOW", destroy)
self.window.deiconify()
self.canvas._tkcanvas.focus_set()
if self._window_dpi.get() != 96:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

why hard-code 96?

self._update_window_dpi()
else:
self.canvas.draw_idle()
if mpl.rcParams['figure.raise_window']:
Expand Down
41 changes: 26 additions & 15 deletionssrc/_tkagg.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -152,6 +152,22 @@ mpl_tk_blit(py::object interp_obj, const char *photo_name,
}

#ifdef WIN32_DLL
void update_tk_window_dpi(HWND hwnd, Tcl_Interp *interp, std::string dpi) {
// This variable naming must match the name used in
// lib/matplotlib/backends/_backend_tk.py:FigureManagerTk.
std::string var_name("window_dpi");
var_name += std::to_string((unsigned long long)hwnd);
if (TCL_SETVAR) {
TCL_SETVAR(interp, var_name.c_str(), dpi.c_str(), 0);
} else if (TCL_SETVAR2) {
TCL_SETVAR2(interp, var_name.c_str(), NULL, dpi.c_str(), 0);
} else {
// This should be prevented at import time, and therefore unreachable.
// But defensively throw just in case.
throw std::runtime_error("Unable to call Tcl_SetVar or Tcl_SetVar2");
}
}

LRESULT CALLBACK
DpiSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
Expand All@@ -165,24 +181,10 @@ DpiSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
// call Python code, we must not also call *any* Tk code from Python.
// So stay with Tcl calls in C only.
{
// This variable naming must match the name used in
// lib/matplotlib/backends/_backend_tk.py:FigureManagerTk.
std::string var_name("window_dpi");
var_name += std::to_string((unsigned long long)hwnd);

// X is high word, Y is low word, but they are always equal.
std::string dpi = std::to_string(LOWORD(wParam));

Tcl_Interp* interp = (Tcl_Interp*)dwRefData;
if (TCL_SETVAR) {
TCL_SETVAR(interp, var_name.c_str(), dpi.c_str(), 0);
} else if (TCL_SETVAR2) {
TCL_SETVAR2(interp, var_name.c_str(), NULL, dpi.c_str(), 0);
} else {
// This should be prevented at import time, and therefore unreachable.
// But defensively throw just in case.
throw std::runtime_error("Unable to call Tcl_SetVar or Tcl_SetVar2");
}
update_tk_window_dpi(hwnd, interp, dpi);
}
return 0;
case WM_NCDESTROY:
Expand DownExpand Up@@ -235,6 +237,15 @@ mpl_tk_enable_dpi_awareness(py::object UNUSED_ON_NON_WINDOWS(frame_handle_obj),
// Per monitor aware means we need to handle WM_DPICHANGED by wrapping
// the Window Procedure, and the Python side needs to trace the Tk
// window_dpi variable stored on interp.
typedef UINT (WINAPI *GetDpiForWindow_t)(HWND);
GetDpiForWindow_t GetDpiForWindowPtr =
(GetDpiForWindow_t)GetProcAddress(user32, "GetDpiForWindow");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Please use the shorter style for casting as in#30615.

if (GetDpiForWindowPtr == NULL) {
FreeLibrary(user32);
return py::cast(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Is completely returning here correct? Is there a case where we may not be able to get the methods for managing the dpi but still need to run thesetWindowSubclass method?

}
UINT dpi = GetDpiForWindowPtr(frame_handle);
update_tk_window_dpi(frame_handle, interp, std::to_string(dpi));
SetWindowSubclass(frame_handle, DpiSubclassProc, 0, (DWORD_PTR)interp);
}
FreeLibrary(user32);
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp