pywin32
方法 1
win32api.GetDeviceCaps() メソッドを使用して、ディスプレイ解像度を取得します。
win32api.GetDC() メソッドを使用して画面全体のデバイス コンテキスト ハンドルを取得し、次に win32api.GetDeviceCaps() メソッドを使用して水平解像度と垂直解像度を取得します。最後に、win32api.ReleaseDC() メソッドを呼び出してデバイス コンテキスト ハンドルを解放する必要があります。
import win32api
import win32con
hdc = win32api.GetDC(0)
screen_width = win32api.GetDeviceCaps(hdc, win32con.HORZRES)
screen_height = win32api.GetDeviceCaps(hdc, win32con.VERTRES)
win32api.ReleaseDC(0, hdc)
print(f"屏幕分辨率为:{screen_width} x {screen_height}")
方法 2
win32api.EnumDisplayMonitors() メソッドを使用して、すべてのモニターを列挙し、その解像度を取得します。
このメソッドは、各要素がディスプレイの長方形の領域を表すリストを返します。長方形の領域からディスプレイの幅と高さを計算して、解像度を決定できます。
import win32api
import win32con
def get_monitor_info():
monitors = []
monitor_enum_proc = lambda hMonitor, hdcMonitor, lprcMonitor, dwData: monitors.append(lprcMonitor)
win32api.EnumDisplayMonitors(None, None, monitor_enum_proc, 0)
return monitors
monitors = get_monitor_info()
for i, monitor in enumerate(monitors):
width = monitor[2] - monitor[0]
height = monitor[3] - monitor[1]
print(f"第{i+1}个显示器分辨率为:{width} x {height}")
方法 3
import win32api
screen_width = win32api.GetSystemMetrics(0)
screen_height = win32api.GetSystemMetrics(1)
print(f"屏幕分辨率为:{screen_width} x {screen_height}")
枕
この方法では、Pillow モジュールがインストールされている必要があることに注意してください。pip install Pillow
コマンドを実行することでインストールできます。ImageGrab モジュールのgrab() メソッドを使用して画面全体のスクリーンショットを取得し、スクリーンショットから幅と高さを読み取り、画面の解像度を取得します。
from PIL import ImageGrab
screen = ImageGrab.grab()
screen_width, screen_height = screen.size
print(f"屏幕分辨率为:{screen_width} x {screen_height}")
ctypes
この方法は Windows オペレーティング システムにのみ適用可能であり、サードパーティのライブラリはインストールされないことに注意してください。Windows ユーザー インターフェイス ライブラリの GetSystemMetrics 関数を呼び出して、画面の解像度を取得します。
import ctypes
user32 = ctypes.windll.user32
screen_width = user32.GetSystemMetrics(0)
screen_height = user32.GetSystemMetrics(1)
print(f"屏幕分辨率为:{screen_width} x {screen_height}")
ピジェットウィンドウ
import pygetwindow as gw
screen = gw.getWindowsWithTitle('')[0]
screen_width, screen_height = screen.size
print(f"屏幕分辨率为:{screen_width} x {screen_height}")
このコードを実行すると、画面解像度が出力されます。pygetwindow
モジュールをインストールする必要があることに注意してください。pip install pygetwindow
コマンドを実行することでインストールできます。
画面情報
from screeninfo import get_monitors
for m in get_monitors():
print(f"屏幕分辨率为:{m.width} x {m.height}")
このコードを実行すると、画面解像度が出力されます。screeninfo
モジュールをインストールする必要があることに注意してください。pip install screeninfo
コマンドを実行することでインストールできます。
トキンター
import tkinter as tk
root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
print(f"屏幕分辨率为:{screen_width} x {screen_height}")
このコードを実行すると、画面解像度が出力されます。GUI 環境で適切に動作する必要があるため、この方法は一部のアプリケーションやサーバー環境には適さない可能性があることに注意してくださいtkinter
。
pyautogui
import pyautogui
screen_width, screen_height = pyautogui.size()
print(f"屏幕分辨率为:{screen_width} x {screen_height}")
このコードを実行すると、画面解像度が出力されます。