[python] Python에서 모니터 해상도를 얻으려면 어떻게해야합니까?

모니터 해상도를 얻는 가장 간단한 방법은 무엇입니까 (가급적 튜플에서)?



답변

Windows의 경우 :

from win32api import GetSystemMetrics

print("Width =", GetSystemMetrics(0))
print("Height =", GetSystemMetrics(1))

고해상도 화면으로 작업하는 경우 Python 인터프리터가 HIGHDPIAWARE인지 확인하십시오.

게시물을 기반으로 합니다 .


답변

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)


답변

이런 이유로 PyPI 모듈 을 만들었습니다 .

pip install screeninfo

코드:

from screeninfo import get_monitors
for m in get_monitors():
    print(str(m))

결과:

monitor(1920x1080+1920+0)
monitor(1920x1080+0+0)

다중 모니터 환경을 지원합니다 . 목표는 크로스 플랫폼이되는 것입니다. 지금은 Cygwin과 X11을 지원하지만 풀 요청은 전적으로 환영합니다.


답변

wxWindows를 사용하는 경우 다음을 수행 할 수 있습니다.

import wx

app = wx.App(False) # the wx.App object must be created first.    
print(wx.GetDisplaySize())  # returns a tuple


답변

이 게시물에 대한 답변에서 직접 발췌 : Tkinter에서 화면 크기를 얻는 방법?

import tkinter as tk

root = tk.Tk()

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()


답변

Windows 8.1에서 ctypes 또는 tk에서 올바른 해상도를 얻지 못합니다. 다른 사람들이 ctypes에 대해 동일한 문제를 겪고 있습니다 . getsystemmetrics가 잘못된 화면 크기
반환합니다 . Windows 8.1에서 높은 DPI 모니터의 올바른 전체 해상도를 얻으려면 SetProcessDPIAware를 호출하고 다음 코드를 사용해야합니다.

import ctypes
user32 = ctypes.windll.user32
user32.SetProcessDPIAware()
[w, h] = [user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)]

아래 전체 세부 정보 :

윈도우가 스케일 된 해상도를보고하기 때문이라는 것을 알았습니다. 파이썬은 기본적으로 ‘시스템 dpi 인식’응용 프로그램 인 것으로 보입니다. DPI 인식 응용 프로그램의 유형은 다음과 같습니다.
http://msdn.microsoft.com/en-us/library/windows/desktop/dn469266%28v=vs.85%29.aspx#dpi_and_the_desktop_scaling_factor

기본적으로 콘텐츠를 전체 모니터 해상도로 표시하여 글꼴을 작게 만드는 대신 글꼴이 충분히 커질 때까지 콘텐츠가 확대됩니다.

내 모니터에서 다음을 얻습니다.
물리적 해상도 : 2560 x 1440 (220 DPI)
보고 된 파이썬 해상도 : 1555 x 875 (158 DPI)

이 Windows 사이트별로 : http://msdn.microsoft.com/en-us/library/aa770067%28v=vs.85%29.aspx
보고 된 시스템 유효 해상도의 공식은 다음과 같습니다. (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


답변

완전성을 위해 Mac OS X

import AppKit
[(screen.frame().size.width, screen.frame().size.height)
    for screen in AppKit.NSScreen.screens()]

모든 화면 크기를 포함하는 튜플 목록을 제공합니다 (여러 모니터가있는 경우).