Skip to content
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

bpo-33656: On Windows, add API call saying that tk scales for DPI. #7137

Merged
merged 7 commits into from
Jun 11, 2018

Conversation

terryjreedy
Copy link
Member

@terryjreedy terryjreedy commented May 26, 2018

On Windows 8.1+ or 10, with DPI compatibility properties of the Python binary
unchanged, and a monitor resolution greater than 96 DPI, this should
make text and lines sharper. It should otherwise have no effect.

Using a magnifier, I determined that the improvement comes from horizontal and
lines being better lined up with the monitor pixels. I checked that this caused no
problem on any Windows buildbot, including the Win7 buildbots. Unlike most
IDLE patches, this one can be easily reverted by users by removing a few lines,
at the top of idlelib/pyshell.py.

https://bugs.python.org/issue33656


import ctypes
try:
ctypes.windll.shcore.SetProcessDpiAwareness(True)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do not use cdll, windll, or oledll. These are LibraryLoader instances that cache the loaded instance of CDLL, WinDLL, or OleDLL, which in turn cache function pointers. This leads to conflicts between projects that use the same libraries with different function prototypes (or lack thereof in this case).

Since this is a function that returns an HRESULT, use shcore = ctypes.OleDLL('shcore'). This will raise OSError if the function returns an error code, which will be the winerror attribute of the exception.

Two errors are documented. It fails with E_ACCESSDENIED (-2147024891, i.e. ctypes.HRESULT(0x80070005).value) if the DPI awareness has already been set by calling this function or in the application manifest. E_INVALIDARG (-2147024809, i.e. ctypes.HRESULT(0x80070057).value) is unlikely, but you should properly define the argument from the PROCESS_DPI_AWARENESS enum. The valid values are PROCESS_DPI_UNAWARE (0), PROCESS_SYSTEM_DPI_AWARE (1), and PROCESS_PER_MONITOR_DPI_AWARE (2).

Copy link
Member Author

Choose a reason for hiding this comment

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

I changed windll.shcore to OleDLL('shcore'). The enum values seem inaccessible and using them would make no real difference. Given the information on the issue (I am about to add more), would you recommend merging this?

@terryjreedy terryjreedy requested a review from zooba June 10, 2018 21:13
@terryjreedy terryjreedy changed the title bpo-33656: turn on DPI awareness in IDLE on Windows bpo-33656: On Windows, add API call saying that tk scales for DPI. Jun 11, 2018
@terryjreedy terryjreedy merged commit 800415e into python:master Jun 11, 2018
@miss-islington
Copy link
Contributor

Thanks @terryjreedy for the PR 🌮🎉.. I'm working now to backport this PR to: 3.6, 3.7.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-7639 is a backport of this pull request to the 3.7 branch.

@bedevere-bot
Copy link

GH-7640 is a backport of this pull request to the 3.6 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 11, 2018
…thonGH-7137)

On Windows 8.1+ or 10, with DPI compatibility properties of the Python binary
unchanged, and a monitor resolution greater than 96 DPI, this should
make text and lines sharper. It should otherwise have no effect.

Using a magnifier, I determined that the improvement comes from horizontal and
lines being better lined up with the monitor pixels. I checked that this call causes
no problem on any Windows buildbot, including the Win7 buildbots. Unlike most
IDLE patches, this one can be easily reverted by users by removing a few lines,
at the top of idlelib/pyshell.py.
(cherry picked from commit 800415e)

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 11, 2018
…thonGH-7137)

On Windows 8.1+ or 10, with DPI compatibility properties of the Python binary
unchanged, and a monitor resolution greater than 96 DPI, this should
make text and lines sharper. It should otherwise have no effect.

Using a magnifier, I determined that the improvement comes from horizontal and
lines being better lined up with the monitor pixels. I checked that this call causes
no problem on any Windows buildbot, including the Win7 buildbots. Unlike most
IDLE patches, this one can be easily reverted by users by removing a few lines,
at the top of idlelib/pyshell.py.
(cherry picked from commit 800415e)

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
miss-islington added a commit that referenced this pull request Jun 11, 2018
…-7137)

On Windows 8.1+ or 10, with DPI compatibility properties of the Python binary
unchanged, and a monitor resolution greater than 96 DPI, this should
make text and lines sharper. It should otherwise have no effect.

Using a magnifier, I determined that the improvement comes from horizontal and
lines being better lined up with the monitor pixels. I checked that this call causes
no problem on any Windows buildbot, including the Win7 buildbots. Unlike most
IDLE patches, this one can be easily reverted by users by removing a few lines,
at the top of idlelib/pyshell.py.
(cherry picked from commit 800415e)

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
miss-islington added a commit that referenced this pull request Jun 11, 2018
…-7137)

On Windows 8.1+ or 10, with DPI compatibility properties of the Python binary
unchanged, and a monitor resolution greater than 96 DPI, this should
make text and lines sharper. It should otherwise have no effect.

Using a magnifier, I determined that the improvement comes from horizontal and
lines being better lined up with the monitor pixels. I checked that this call causes
no problem on any Windows buildbot, including the Win7 buildbots. Unlike most
IDLE patches, this one can be easily reverted by users by removing a few lines,
at the top of idlelib/pyshell.py.
(cherry picked from commit 800415e)

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
if sys.platform == 'win32':
import ctypes
try:
ctypes.OleDLL('shcore').SetProcessDpiAwareness(1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please define PROCESS_SYSTEM_DPI_AWARE = 1 instead of using a magic number. It wouldn't hurt to quote MSDN in a brief comment that explains this value and also the MSDN URL for the PROCESS_DPI_AWARENESS enumeration.

Copy link
Member Author

@terryjreedy terryjreedy Jun 11, 2018

Choose a reason for hiding this comment

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

I agree. I made a new PR with both changes. It should make it into today's releases.
#7642.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants