Reputation: 93
So I was trying to make a game with python and pygame but I noticed that I couldn't make a high resolution display because when I tried to make a display with more pixels, the pygame window was too big for my 4k (3840x2160) monitor. I should note that my monitor is connected to an old Dell laptop with a resolution of (1366x768). But when I entered this: print(pygame.display.list_modes())
it told me that I could use resolutions up to 4k and not just up to the resolution of my laptop. After a lot of searching and trying I accepted the fact that my game will be low resolution and moved on. As I continued coding the game I wanted to have a pop-up window so I imported pyautogui and my pygame window suddenly became much smaller. BOOM problem solved. I increased the resolution and I had no problems, my game was now running at a very high resolution! I was very confused so I made a very simple pygame program so I could test this and it actually worked. This is low quality and can't fit in my screen:
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((3000, 1500))
font = pygame.font.Font('font.otf', 50)
while True:
screen.fill((255, 255, 255))
txt = font.render("hello", True, (0, 0, 0))
screen.blit(txt, (100, 100))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
And this is high resolution and does fit in my screen:
import pygame
import sys
import pyautogui
pygame.init()
screen = pygame.display.set_mode((3000, 1500))
font = pygame.font.Font('font.otf', 50)
while True:
screen.fill((255, 255, 255))
txt = font.render("hello", True, (0, 0, 0))
screen.blit(txt, (100, 100))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
I don't even need to use pyautogui! Can someone explain this to me? Thanks
Upvotes: 7
Views: 1235
Reputation: 1295
After a bunch of source diving I believe I have found the solution: pyautogui imports pyscreeze
for the functions center, grab, pixel, pixelMatchesColor, screenshot
. On lines 63 to 71 of pyscreeze/__init__.py
is the following:
if sys.platform == 'win32':
# On Windows, the monitor scaling can be set to something besides normal 100%.
# PyScreeze and Pillow needs to account for this to make accurate screenshots.
# TODO - How does macOS and Linux handle monitor scaling?
import ctypes
try:
ctypes.windll.user32.SetProcessDPIAware()
except AttributeError:
pass # Windows XP doesn't support monitor scaling, so just do nothing.
The above code calls SetProcessDPIAware, which is equivalent to the following:
System DPI aware. This window does not scale for DPI changes. It will query for the DPI once and use that value for the lifetime of the process. If the DPI changes, the process will not adjust to the new DPI value. It will be automatically scaled up or down by the system when the DPI changes from the system value.
If want to get the same effect without pyautogui
you can just include the above call to SetProcessDPIAware
in your code.
Upvotes: 5