psutil来获取屏幕分辨率
时间: 2024-12-15 15:13:39 浏览: 2
实际上,`psutil`库主要用于系统资源监控,如CPU、内存、磁盘和网络等,并不是用于直接获取屏幕分辨率的官方工具。如果你想要获取屏幕分辨率,通常应该使用操作系统特定的API或命令行工具。
例如,在Linux系统中,可以使用`subprocess`模块结合`xdpyinfo`命令来获取屏幕信息:
```python
import subprocess
def get_screen_resolution():
try:
geometry = subprocess.check_output(['xdpyinfo', '-display:', ':0']).decode()
for line in geometry.split('\n'):
if 'dimensions:' in line:
width, height = [int(dim) for dim in line.strip().split()[3:]]
break
except FileNotFoundError:
print("xdpyinfo not found. Please check if it's installed and accessible.")
return None
return width, height
width, height = get_screen_resolution()
if width and height:
print(f"Screen resolution: {width} x {height}")
else:
print("Failed to get the screen resolution.")
#
阅读全文