8000 gh-119459: Fix HiDPI blurring of every program window that using tkinter by Wulian233 · Pull Request #119902 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-119459: Fix HiDPI blurring of every program window that using tkinter #119902

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Ensures that the window size is not reduced due to DPI adjustments, m…
…aintaining the original size
  • Loading branch information
Wulian233 committed Jun 22, 2024
commit c9f9edf94d1f174ac80dea3734a7ad16b386a120
36 changes: 20 additions & 16 deletions Lib/tkinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2464,22 +2464,6 @@ def __init__(self, screenName=None, baseName=None, className='Tk',
if not sys.flags.ignore_environment:
# Issue #16248: Honor the -E flag to avoid code injection.
self.readprofile(baseName, className)
# Fix for HiDPI screens on Windows
# CALL BEFORE ANY TK OPERATIONS!
# URL for arguments for the ...Awareness call below.
# https://msdn.microsoft.com/en-us/library/windows/desktop/dn280512(v=vs.85).aspx
if sys.platform == 'win32':
import ctypes
try:
# >= win 8.1
PROCESS_SYSTEM_DPI_AWARE: int = 1 # Int required
ctypes.windll.shcore.SetProcessDpiAwareness(PROCESS_SYSTEM_DPI_AWARE)
except (ImportError, AttributeError, OSError):
try:
# win 8.0 or less
ctypes.windll.user32.SetProcessDPIAware()
except (ImportError, AttributeError, OSError):
pass

def loadtk(self):
if not self._tkloaded:
Expand Down Expand Up @@ -2512,6 +2496,26 @@ def _loadtk(self):
_default_root = self
self.protocol("WM_DELETE_WINDOW", self.destroy)

# Fix for HiDPI screens on Windows
# CALL BEFORE ANY TK OPERATIONS!
# URL for arguments for the ...Awareness call below.
# https://msdn.microsoft.com/en-us/library/windows/desktop/dn280512(v=vs.85).aspx
if sys.platform == 'win32':
Copy link
Contributor

Choose a reason for hiding this comment

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

In my humble opinion, can you guarantee that this code will not be called repeatedly? Maybe it only needs to be called once?

Copy link
Contributor

Choose a reason for hiding this comment

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

Add a print statement to your code and just try the code below:

from tkinter import messagebox

messagebox.showinfo("info", "test")
messagebox.showinfo("info", "test")

Then you'll find that it runs twice.

import ctypes
try:
ctypes.windll.user32.SetProcessDPIAware()
# >= win 8.1 required
# Ensures that the window size is not reduced due to DPI adjustments,
# maintaining the original size
ScaleFactor = ctypes.windll.shcore.GetScaleFactorForDevice(0)
self.tk.call('tk', 'scaling', ScaleFactor / 75)
except (ImportError, AttributeError, OSError):
# <= win 8
try:
ctypes.windll.user32.SetProcessDPIAware()
except (ImportError, AttributeError, OSError):
pass

def destroy(self):
"""Destroy this and all descendants widgets. This will
end the application of this Tcl interpreter."""
Expand Down
0