How do I get monitor resolution in Python?
On Windows:
from win32api import GetSystemMetrics
print("Width =", GetSystemMetrics(0))
print("Height =", GetSystemMetrics(1))
If you are working with high resolution screen, make sure your python interpreter is HIGHDPIAWARE.
Based on this post.
Getting Display resolution with python isn't accurate
This problem is caused by the scaling setting that is set to 125%.
So I found 2 solutions:
The First one answered by @spacether in @rectangletangle's question:
import ctypes
user32 = ctypes.windll.user32
user32.SetProcessDPIAware()
Width = user32.GetSystemMetrics(0)
Height = user32.GetSystemMetrics(1)
The Second one posted on this website:
import win32con, win32gui, win32print
def get_dpi():
hDC = win32gui.GetDC(0)
HORZRES = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES)
VERTRES = win32print.GetDeviceCaps(hDC, win32con.DESKTOPVERTRES)
return HORZRES,VERTRES
Getting Monitor resolution in Python on Ubuntu
I assume you're a GUI toolkit. Why else would you be interested in the screen dimensions?
Check out gtk.gdk.screen_width()
and gtk.gdk.screen_height()
from PyGTK. Something similar should be available for QT.
Number of pixels of screen python
Borrowing from this question: How do I get monitor resolution in Python?, there are several options. Here's one with Tkinter:
import Tkinter as tk
root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
print screen_width * screen_height # (e.g.) 3686400 = 2560*1440
That other post has a lot of different ways to do it, including getting information about multi-monitor setup, Windows OS implementations, DPI, etc.
How do I get the screen / display resolution?
I depends of the backend you use.
For example if you use the Winit / Glutin backend you can use the MonitorHandle
struct, which has a size()
method.
Docs:https://docs.rs/winit/0.26.1/winit/monitor/struct.MonitorHandle.html
or for Glutin https://docs.rs/glutin/0.28.0/glutin/window/struct.Window.html#method.available_monitors
The Glutin module also has a dpi
module that can provide information. https://docs.rs/glutin/0.28.0/glutin/dpi/index.html
If you use SDL2 backend, you could take a look at the sdl2_sys module SDL_HINT_RENDER_LOGICAL_SIZE https://docs.rs/sdl2-sys/0.35.2/sdl2_sys/constant.SDL_HINT_RENDER_LOGICAL_SIZE_MODE.html
How to get the resolution of a monitor in Pygame?
You can use pygame.display.Info()
:
The docs say:
current_h, current_w: Height and width of the current video mode, or of the
desktop mode if called before the display.set_mode is called.
(current_h, current_w are available since SDL 1.2.10, and pygame
1.8.0) They are -1 on error, or if an old SDL is being used.1.8.0)
pygame.display.Info()
creates an Info Object with the attributes current_h
and current_w
.
Create the Info Object before you call display.set_mode
and then call display.set_mode
with current_h
and current_w
from the object.
Example:
infoObject = pygame.display.Info()
pygame.display.set_mode((infoObject.current_w, infoObject.current_h))
How to get all my system supported screen resolutions in python
For most things that are Windows related, you can use the Windows API directly using the pywin32
module.
So, for getting all possible screen resolutions, you can use the EnumDisplaySettings
function.
Simple example:
import win32api
i=0
res=set()
try:
while True:
ds=win32api.EnumDisplaySettings(None, i)
res.add(f"{ds.PelsWidth}x{ds.PelsHeight}")
i+=1
except: pass
print(res)
Result:
{'1920x1080', '1152x864', '1176x664', '1768x992', '800x600', '720x576', '1600x1200', '1680x1050', '1280x720', '1280x1024', '1280x800', '1440x900', '1366x768', '1280x768', '640x480', '720x480', '1024x768', '1360x768'}
But if you want to use pygame, you have to initialize the pygame module first by calling pygame.init()
, like this:
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
pygame 2.0.0.dev10 (SDL 2.0.12, python 3.7.3)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> pygame.init()
(6, 0)
>>> pygame.display.list_modes()
[(1920, 1080), (1920, 1080), (1920, 1080), (1920, 1080), (1920, 1080), (1920, 1080), (1768, 992), (1768, 992), (1768, 992), (1680, 1050), (1680, 1050), (1600, 1200), (1440, 900), (1440, 900), (1366, 768), (1366, 768), (1360, 768), (1360, 768), (1280, 1024), (1280, 1024), (1280, 800), (1280, 800), (1280, 768), (1280, 768), (1280, 720), (1280, 720), (1280, 720), (1176, 664), (1176, 664), (1176, 664), (1152, 864), (1024, 768), (1024, 768), (1024, 768), (800, 600), (800, 600), (800, 600), (800, 600), (720, 576), (720, 480), (720, 480), (640, 480), (640, 480), (640, 480), (640, 480)]
>>>
How can I get the screen size in Tkinter?
import tkinter as tk
root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
How to get total screen size in Python / GTK without using deprecated Gdk.Screen.get_width()?
Based on the commit which removed those API calls, the algorithm that GDK uses to compute the screen size seems to be basically this:
def get_screen_size(display):
mon_geoms = [
display.get_monitor(i).get_geometry()
for i in range(display.get_n_monitors())
]
x0 = min(r.x for r in mon_geoms)
y0 = min(r.y for r in mon_geoms)
x1 = max(r.x + r.width for r in mon_geoms)
y1 = max(r.y + r.height for r in mon_geoms)
return x1 - x0, y1 - y0
# example use
print(get_screen_size(Gdk.Display.get_default()))
Related Topics
Python 3 Importerror: No Module Named 'Configparser'
How to Check If All Elements of a List Match a Condition
Execute Code When Django Starts Once Only
Pygame.Event.Get() Not Returning Any Events When Inside a Thread
Changing User Agent on Urllib2.Urlopen
Why Is Python 3.X's Super() Magic
How to Make Smooth Movement in Pygame
"Fire and Forget" Python Async/Await
How to Sort Two Lists (Which Reference Each Other) in the Exact Same Way
How to Use Xpath with Beautifulsoup
Adding Days to a Date in Python
Set Matplotlib Colorbar Size to Match Graph
Python Socket Not Receiving Without Sending