Reputation: 52911
What is the simplest way to get monitor resolution (preferably in a tuple)?
Upvotes: 205
Views: 363353
Reputation: 870
If you are using PySimpleGUI you can use:
window.get_screen_size()
This returns a tuple giving the size of the main monitor sreen (even if your window is located on a second monitor screen).
For example:
import PySimpleGUI as sg
layout = [[sg.Button('Print screen size')]]
window = sg.Window('Test', layout)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
if event == 'Print screen size':
print(window.get_screen_size())
window.close()
Upvotes: 0
Reputation: 375484
If you're using wxWindows (having installed wxPython), you can simply do:
import wx
app = wx.App(False) # the wx.App object must be created first.
print(wx.GetDisplaySize()) # returns a tuple
Upvotes: 48
Reputation: 520
Here is another one which hasn't been mentioned yet. Windows only - uses user32.EnumDisplayMonitors - Pure Python
def get_monitors_resolution():
user32 = ctypes.windll.user32
def _get_monitors_resolution():
monitors = []
monitor_enum_proc = ctypes.WINFUNCTYPE(
ctypes.c_int, ctypes.c_ulong, ctypes.c_ulong, ctypes.POINTER(ctypes.wintypes.RECT), ctypes.c_double)
def callback(hMonitor, hdcMonitor, lprcMonitor, dwData):
monitors.append((lprcMonitor.contents.right - lprcMonitor.contents.left,
lprcMonitor.contents.bottom - lprcMonitor.contents.top))
return 1
user32.EnumDisplayMonitors(None, None, monitor_enum_proc(callback), 0)
return monitors
resolutions = _get_monitors_resolution()
allmonitors = {}
for i, res in enumerate(resolutions):
allmonitors[i] = {'width': res[0], 'height': res[1]}
return allmonitors
get_monitors_resolution()
Out[2]: {0: {'width': 1920, 'height': 1080}, 1: {'width': 1920, 'height': 1080}}
Upvotes: 0
Reputation: 14811
I created a PyPI module for this reason:
pip install screeninfo
The code:
from screeninfo import get_monitors
for m in get_monitors():
print(str(m))
Result:
Monitor(x=3840, y=0, width=3840, height=2160, width_mm=1420, height_mm=800, name='HDMI-0', is_primary=False)
Monitor(x=0, y=0, width=3840, height=2160, width_mm=708, height_mm=399, name='DP-0', is_primary=True)
It supports multi monitor environments. Its goal is to be cross platform; for now it supports Cygwin and X11 but pull requests are totally welcome.
Upvotes: 197
Reputation: 1690
Late to the game. I think I found the cross-platform using the dependence-free library mss
that supports multiple monitors (https://pypi.org/project/mss/):
import mss
sct=mss.mss()
sct.monitors
Then you get something like this:
[{'left': -1440, 'top': 0, 'width': 4000, 'height': 1080},
{'left': 0, 'top': 0, 'width': 2560, 'height': 1080},
{'left': -1440, 'top': 180, 'width': 1440, 'height': 900}]
The element 0 is the virtual screen combining all monitors. The element 1 is the primary monitor, and element 2 the second monitor.
Upvotes: 4
Reputation: 911
Try using pyautogui.size()
!
import pyautogui #pip install pyautogui
x = pyautogui.size()[0] # getting the width of the screen
y = pyautogui.size()[1] # getting the height of the screen
print(x,y)
Upvotes: 0
Reputation: 1382
A cross-platform and easy way to do this is by using Tkinter that comes with nearly all the Python versions, so you don't have to install anything:
import tkinter
root = tkinter.Tk()
root.withdraw()
WIDTH, HEIGHT = root.winfo_screenwidth(), root.winfo_screenheight()
Upvotes: 20
Reputation: 40492
Using Linux, the simplest way is to execute Bash command
xrandr | grep '*'
And parse its output using a regular expression.
Also you can do it through Pygame: Pygame - Get screen size
Upvotes: 14
Reputation: 666
Instead of a regular expression, take the first line and take out the current resolution values.
Current resolution of display :0
>>> screen = os.popen("xrandr -q -d :0").readlines()[0]
>>> print screen
Screen 0: minimum 320 x 200, current 1920 x 1080, maximum 1920 x 1920
>>> width = screen.split()[7]
>>> print width
1920
>>> height = screen.split()[9][:-1]
>>> print height
1080
>>> print "Current resolution is %s x %s" % (width,height)
Current resolution is 1920 x 1080
This was done on xrandr 1.3.5, I don't know if the output is different on other versions, but this should make it easy to figure out.
Upvotes: 3
Reputation: 101
X Window version:
#!/usr/bin/python
import Xlib
import Xlib.display
resolution = Xlib.display.Display().screen().root.get_geometry()
print str(resolution.width) + "x" + str(resolution.height)
Upvotes: 10
Reputation: 4700
Taken directly from an answer to the post 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()
Upvotes: 68
Reputation: 3163
If you are using the Qt toolkit, specifically PySide, you can do the following:
from PySide import QtGui
import sys
app = QtGui.QApplication(sys.argv)
screen_rect = app.desktop().screenGeometry()
width, height = screen_rect.width(), screen_rect.height()
Upvotes: 17
Reputation: 2699
On Windows 8.1 I am not getting the correct resolution from either ctypes or tk. Other people are having this same problem for ctypes: getsystemmetrics returns wrong screen size
To get the correct full resolution of a high DPI monitor on Windows 8.1, one must call SetProcessDPIAware and use the following code:
import ctypes
user32 = ctypes.windll.user32
user32.SetProcessDPIAware()
[w, h] = [user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)]
Full Details Below:
I found out that this is because windows is reporting a scaled resolution. It appears that python is by default a 'system dpi aware' application. Types of DPI aware applications are listed here: High DPI Desktop Application Development on Windows
Basically, rather than displaying content the full monitor resolution, which would make fonts tiny, the content is scaled up until the fonts are big enough.
On my monitor I get:
Physical resolution: 2560 x 1440 (220 DPI)
Reported python resolution: 1555 x 875 (158 DPI)
Per this Windows site: Adjusting Scale for Higher DPI Screens. The formula for reported system effective resolution is:
(reported_px*current_dpi)/(96 dpi) = physical_px
I'm able to get the correct full screen resolution, and current DPI with the below code. Note that I call SetProcessDPIAware() to allow the program to see the real resolution.
import tkinter as tk
root = tk.Tk()
width_px = root.winfo_screenwidth()
height_px = root.winfo_screenheight()
width_mm = root.winfo_screenmmwidth()
height_mm = root.winfo_screenmmheight()
# 2.54 cm = in
width_in = width_mm / 25.4
height_in = height_mm / 25.4
width_dpi = width_px/width_in
height_dpi = height_px/height_in
print('Width: %i px, Height: %i px' % (width_px, height_px))
print('Width: %i mm, Height: %i mm' % (width_mm, height_mm))
print('Width: %f in, Height: %f in' % (width_in, height_in))
print('Width: %f dpi, Height: %f dpi' % (width_dpi, height_dpi))
import ctypes
user32 = ctypes.windll.user32
user32.SetProcessDPIAware()
[w, h] = [user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)]
print('Size is %f %f' % (w, h))
curr_dpi = w*96/width_px
print('Current DPI is %f' % (curr_dpi))
Which returned:
Width: 1555 px, Height: 875 px
Width: 411 mm, Height: 232 mm
Width: 16.181102 in, Height: 9.133858 in
Width: 96.099757 dpi, Height: 95.797414 dpi
Size is 2560.000000 1440.000000
Current DPI is 158.045016
I am running Windows 8.1 with a 220 DPI capable monitor. My display scaling sets my current DPI to 158.
I'll use the 158 to make sure my Matplotlib plots are the right size with:
from pylab import rcParams
rcParams['figure.dpi'] = curr_dpi
Upvotes: 42
Reputation: 856
If you are working on Windows OS, you can use OS module to get it:
import os
cmd = 'wmic desktopmonitor get screenheight, screenwidth'
size_tuple = tuple(map(int,os.popen(cmd).read().split()[-2::]))
It will return a tuple (Y,X) where Y is the vertical size and X is the horizontal size. This code works on Python 2 and Python 3
UPDATE
For Windows 8/8.1/10, the above answer doesn't work, use the next one instead:
import os
cmd = "wmic path Win32_VideoController get CurrentVerticalResolution,CurrentHorizontalResolution"
size_tuple = tuple(map(int,os.popen(cmd).read().split()[-2::]))
Upvotes: 4
Reputation: 94
On Linux:
import subprocess
import re
def getScreenDimensions():
xrandrOutput = str(subprocess.Popen(['xrandr'], stdout=subprocess.PIPE).communicate()[0])
matchObj = re.findall(r'current\s(\d+) x (\d+)', xrandrOutput)
if matchObj:
return (int(matchObj[0][0]), int(matchObj[0][1]))
screenWidth, screenHeight = getScreenDimensions()
print(f'{screenWidth} x {screenHeight}')
Upvotes: 5
Reputation: 622
A lot of these answers use tkinter to find the screen height/width (resolution), but sometimes it is necessary to know the dpi of your screen cross-platform compatible. This answer is from this link and left as a comment on another post, but it took hours of searching to find. I have not had any issues with it yet, but please let me know if it does not work on your system!
import tkinter
root = tkinter.Tk()
dpi = root.winfo_fpixels('1i')
The documentation for this says:
winfo_fpixels(number)
# Return the number of pixels for the given distance NUMBER (e.g. "3c") as float
A distance number is a digit followed by a unit, so 3c means 3 centimeters, and the function gives the number of pixels on 3 centimeters of the screen (as found here). So to get dpi, we ask the function for the number of pixels in 1 inch of screen ("1i").
Upvotes: 4
Reputation: 111
Utility script using pynput
library. Posting here for ref.:
from pynput.mouse import Controller as MouseController
def get_screen_size():
"""Utility function to get screen resolution"""
mouse = MouseController()
width = height = 0
def _reset_mouse_position():
# Move the mouse to the top left of
# the screen
mouse.position = (0, 0)
# Reset mouse position
_reset_mouse_position()
count = 0
while 1:
count += 1
mouse.move(count, 0)
# Get the current position of the mouse
left = mouse.position[0]
# If the left doesn't change anymore, then
# that's the screen resolution's width
if width == left:
# Add the last pixel
width += 1
# Reset count for use for height
count = 0
break
# On each iteration, assign the left to
# the width
width = left
# Reset mouse position
_reset_mouse_position()
while 1:
count += 1
mouse.move(0, count)
# Get the current position of the mouse
right = mouse.position[1]
# If the right doesn't change anymore, then
# that's the screen resolution's height
if height == right:
# Add the last pixel
height += 1
break
# On each iteration, assign the right to
# the height
height = right
return width, height
>>> get_screen_size()
(1920, 1080)
Upvotes: 0
Reputation: 73
For Linux, you can use this:
import gi
gi.require_version("Gdk", "3.0")
from gi.repository import Gdk
s = Gdk.Screen.get_default()
screen_width = s.get_width()
screen_height = s.get_height()
print(screen_width)
print(screen_height)
Upvotes: 1
Reputation: 665
For later versions of PyGtk:
import gi
gi.require_version("Gdk", "3.0")
from gi.repository import Gdk
display = Gdk.Display.get_default()
n_monitors = display.get_n_monitors()
print("there are %d monitors" % n_monitors)
for m in range(n_monitors):
monitor = display.get_monitor(m)
geometry = monitor.get_geometry()
print("monitor %d: %d x %d" % (m, geometry.width, geometry.height))
Upvotes: 1
Reputation: 26886
Expanding on @user2366975's answer, to get the current screen size in a multi-screen setup using Tkinter (code in Python 2/3):
try:
# for Python 3
import tkinter as tk
except ImportError:
# for Python 2
import Tkinter as tk
def get_curr_screen_geometry():
"""
Workaround to get the size of the current screen in a multi-screen setup.
Returns:
geometry (str): The standard Tk geometry string.
[width]x[height]+[left]+[top]
"""
root = tk.Tk()
root.update_idletasks()
root.attributes('-fullscreen', True)
root.state('iconic')
geometry = root.winfo_geometry()
root.destroy()
return geometry
(Should work cross-platform, tested on Linux only)
Upvotes: 18
Reputation: 117
It's a little troublesome for retina screen, i use tkinter to get the fake size, use pilllow grab to get real size :
import tkinter
root = tkinter.Tk()
resolution_width = root.winfo_screenwidth()
resolution_height = root.winfo_screenheight()
image = ImageGrab.grab()
real_width, real_height = image.width, image.height
ratio_width = real_width / resolution_width
ratio_height = real_height/ resolution_height
Upvotes: 1
Reputation: 4316
Old question but this is missing. I'm new to python so please tell me if this is a "bad" solution. This solution is supported for Windows and MacOS only and it works just for the main screen - but the os is not mentioned in the question.
Measure the size by taking a screenshot. As the screensize should not change this has to be done only once. There are more elegant solutions if you have a gui toolkit like GTK, wx, ... installed.
see Pillow
pip install Pillow
from PIL import ImageGrab
img = ImageGrab.grab()
print (img.size)
Upvotes: 20
Reputation: 4593
On Linux we can use subprocess module
import subprocess
cmd = ['xrandr']
cmd2 = ['grep', '*']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
p2 = subprocess.Popen(cmd2, stdin=p.stdout, stdout=subprocess.PIPE)
p.stdout.close()
resolution_string, junk = p2.communicate()
resolution = resolution_string.split()[0]
resolution = resolution.decode("utf-8")
width = int(resolution.split("x")[0].strip())
heigth = int(resolution.split("x")[1].strip())
Upvotes: 0
Reputation: 2848
In Windows, you can also use ctypes with GetSystemMetrics()
:
import ctypes
user32 = ctypes.windll.user32
screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
so that you don't need to install the pywin32 package; it doesn't need anything that doesn't come with Python itself.
For multi-monitor setups, you can retrieve the combined width and height of the virtual monitor:
import ctypes
user32 = ctypes.windll.user32
screensize = user32.GetSystemMetrics(78), user32.GetSystemMetrics(79)
Upvotes: 157
Reputation: 8259
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.
Upvotes: 106
Reputation: 11605
You could use PyMouse. To get the screen size just use the screen_size()
attribute:
from pymouse import PyMouse
m = PyMouse()
a = m.screen_size()
a
will return a tuple, (X, Y)
, where X
is the horizontal position and Y
is the vertical position.
Link to function in documentation.
Upvotes: 1
Reputation: 702
In case you have PyQt4 installed, try the following code:
from PyQt4 import QtGui
import sys
MyApp = QtGui.QApplication(sys.argv)
V = MyApp.desktop().screenGeometry()
h = V.height()
w = V.width()
print("The screen resolution (width X height) is the following:")
print(str(w) + "X" + str(h))
For PyQt5, the following will work:
from PyQt5 import QtWidgets
import sys
MyApp = QtWidgets.QApplication(sys.argv)
V = MyApp.desktop().screenGeometry()
h = V.height()
w = V.width()
print("The screen resolution (width X height) is the following:")
print(str(w) + "X" + str(h))
Upvotes: 6
Reputation: 2365
Using pygame:
import pygame
pygame.init()
infos = pygame.display.Info()
screen_size = (infos.current_w, infos.current_h)
However, if you're trying to set your window to the size of the screen, you might just want to do:
pygame.display.set_mode((0,0),pygame.FULLSCREEN)
to set your display to fullscreen mode. [2]
Upvotes: 3
Reputation: 4901
Another version using xrandr
:
import re
from subprocess import run, PIPE
output = run(['xrandr'], stdout=PIPE).stdout.decode()
result = re.search(r'current (\d+) x (\d+)', output)
width, height = map(int, result.groups()) if result else (800, 600)
Upvotes: 2
Reputation: 77
Try pyautogui:
import pyautogui
resolution = pyautogui.size()
print(resolution)
Upvotes: 6