获取显示器分辨率的最简单方法是什么(最好返回一个元组)?
我出于这个原因创建了一个PyPI模块:
pip install screeninfo
代码:
from screeninfo import get_monitors
for m in get_monitors():
print(str(m))
结果:
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)
它支持多显示器环境。它的目标是跨平台;目前它支持Cygwin和X11,但欢迎拉取请求。
pip install cython pyobjus
)。 - George Profenza在Windows中,您还可以使用ctypes与GetSystemMetrics()
一起使用:
import ctypes
user32 = ctypes.windll.user32
screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
这样你就不需要安装pywin32包了;它不需要除Python本身以外的任何东西。
对于多监视器设置,您可以检索虚拟监视器的组合宽度和高度:
import ctypes
user32 = ctypes.windll.user32
screensize = user32.GetSystemMetrics(78), user32.GetSystemMetrics(79)
GetSystemMetrics(78)
和GetSystemMetrics(79)
分别返回什么? - Stevoisiak在Windows操作系统中:
from win32api import GetSystemMetrics
print("Width =", GetSystemMetrics(0))
print("Height =", GetSystemMetrics(1))
如果您使用高分辨率的屏幕,请确保您的Python解释器是HIGHDPIAWARE。
参考此帖子。
以下内容摘自回答帖子:如何在Tkinter中获取屏幕尺寸?
import tkinter as tk
root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
import wx
app = wx.App(False) # the wx.App object must be created first.
print(wx.GetDisplaySize()) # returns a tuple
app = wx.App(False)
,否则你会得到“必须先创建wx.App对象”的错误。26个投票,没有人检查吗?在Windows中工作时不需要将wx实例分配给变量吗? - m3nda在Windows 8.1中,我无法从ctypes或tk获得正确的分辨率。其他人也遇到了ctypes的同样问题:getsystemmetrics返回错误的屏幕大小
为了获取高DPI显示器的正确全分辨率,在Windows 8.1上,必须调用SetProcessDPIAware并使用以下代码:
import ctypes
user32 = ctypes.windll.user32
user32.SetProcessDPIAware()
[w, h] = [user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)]
以下是完整细节:
我发现这是因为Windows报告了一个缩放分辨率。似乎Python默认是一个“系统DPI感知”应用程序。DPI感知应用程序的类型列于此处: 在 Windows 上进行高 DPI 桌面应用程序开发
基本上,与其显示全屏幕分辨率的内容,这会使字体变得很小,而是将内容缩放,直到字体足够大。
在我的显示器上,我得到:
物理分辨率:2560 x 1440 (220 DPI)
报告的 Python 分辨率:1555 x 875 (158 DPI)
根据这个 Windows 网站: 为高 DPI 屏幕调整比例。 所报告的系统有效分辨率的公式是:
(reported_px*current_dpi)/(96 dpi) = physical_px
我可以通过下面的代码获取正确的全屏分辨率和当前 DPI。 请注意,我调用 SetProcessDPIAware() 来允许程序查看实际分辨率。
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))
哪个返回:
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
我正在使用一台支持220 DPI的显示器运行Windows 8.1。 我的显示缩放将当前DPI设置为158。
我将使用158来确保我的Matplotlib图表具有正确的大小:
from pylab import rcParams
rcParams['figure.dpi'] = curr_dpi
Tkinter
使用了正确的像素,但不知道dpi缩放,因此报告了错误的毫米尺寸。 - Primerctypes.windll.shcore.SetProcessDpiAwareness(2)
返回错误 OSError: [WinError 126] 找不到指定的模块
。 - Adrian Keister为了完整起见,Mac OS X
import AppKit
[(screen.frame().size.width, screen.frame().size.height)
for screen in AppKit.NSScreen.screens()]
会给你一个元组列表,包含所有屏幕大小 (如果存在多个显示器)
跨平台且简单的方法是使用随附于几乎所有Python版本的Tkinter,因此您无需安装任何东西:
import tkinter
root = tkinter.Tk()
root.withdraw()
WIDTH, HEIGHT = root.winfo_screenwidth(), root.winfo_screenheight()
虽然这是一个老问题,但却没有解答。我刚开始学习Python,请告诉我这是否是一种“不好”的解决方案。 这个解决方案仅支持Windows和MacOS,并且仅适用于主屏幕 - 但问题中没有提到操作系统。
通过截屏来测量大小。由于屏幕大小不应该改变,因此只需执行一次即可。 如果您安装了GUI工具包(如GTK、wx等),则有更优雅的解决方案。
请参见Pillow
pip install Pillow
from PIL import ImageGrab
img = ImageGrab.grab()
print (img.size)
Tkinter
模块,您可以按照这种方式进行操作。它是Python标准库的一部分,在大多数Unix和Windows平台上都可以使用。 - martineau