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
Show file tree
Hide file tree
Changes from all 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
4 changes: 0 additions & 4 deletions Lib/idlelib/pyshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
"Your Python may not be configured for Tk. **", file=sys.__stderr__)
raise SystemExit(1)

if sys.platform == 'win32':
from idlelib.util import fix_win_hidpi
fix_win_hidpi()

from tkinter import messagebox

from code import InteractiveInterpreter
Expand Down
15 changes: 0 additions & 15 deletions Lib/idlelib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,11 @@
* std streams (pyshell, run),
* warning stuff (pyshell, run).
"""
import sys

# .pyw is for Windows; .pyi is for typing stub files.
# The extension order is needed for iomenu open/save dialogs.
py_extensions = ('.py', '.pyw', '.pyi')


# 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': # pragma: no cover
def fix_win_hidpi(): # Called in pyshell and turtledemo.
try:
import ctypes
PROCESS_SYSTEM_DPI_AWARE = 1 # Int required.
ctypes.OleDLL('shcore').SetProcessDpiAwareness(PROCESS_SYSTEM_DPI_AWARE)
except (ImportError, AttributeError, OSError):
pass


if __name__ == '__main__':
from unittest import main
main('idlelib.idle_test.test_util', verbosity=2)
20 changes: 20 additions & 0 deletions Lib/tkinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2496,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
4 changes: 0 additions & 4 deletions Lib/turtledemo/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@
import turtle
from turtledemo import __doc__ as about_turtledemo

if sys.platform == 'win32':
from idlelib.util import fix_win_hidpi
fix_win_hidpi()

demo_dir = os.path.dirname(os.path.abspath(__file__))
darwin = sys.platform == 'darwin'
STARTUP = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix HiDPI blurring of every program window that using tkinter. Patch by
Wulian233
Loading