Pygame多个moni

2024-04-20 05:43:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试获取应用程序中当前监视器大小的值。 我用GNOME在ubuntu 16.04上。
问题是我有一个上网本显示器和一个外部显示器,所以如果我试着做如下事情:

info_object = pygame.display.Info() # info_object.current_w / info_object.current_h
screen = pygame.display.set_mode((info_object.current_w, info_object.current_h))

我发现屏幕的宽度是上网本显示器+外部显示器,所以分辨率如下:

(3286, 1080)

所以我的另一个尝试是获取有关监视器使用的信息pygame.display.list_模式()为了获得一些显示分辨率设置,但我得到的列表如下:

[(3286, 1080), (1366, 768), (1360, 768), (1024, 768), (960, 720), (960, 600), (960, 540), (928, 696), (896, 672), (840, 525), (800, 600), (800, 512), (720, 450), (700, 525), (680, 384), (640, 512), (640, 480), (576, 432), (512, 384), (400, 300), (320, 240)]

但仍然不知道当前的“活动”监视器是什么。
如果我在我的上网本显示器上打开我的程序,我希望得到该显示器的分辨率,而如果我在外部显示器上打开它,我希望它的分辨率不是一加一的。

我怎么才能做到呢?在


Tags: info应用程序objectubuntudisplay分辨率current事情
1条回答
网友
1楼 · 发布于 2024-04-20 05:43:55

您可以通过shell使用x11实用程序。下面的脚本输出活动屏幕的宽度和高度(鼠标光标所在的屏幕)。您可能需要安装xdotool。在

#!/usr/bin/env bash
## find the resolution of the active screen
## based on Adam Bowen's solution at:
## https://superuser.com/questions/603528/how-to-get-the-current-monitor-resolution-or-monitor-name-lvds-vga1-etc
##
OFFSET_RE="[+-]([-0-9]+)[+-]([-0-9]+)"
# find offset in window data in form 143x133-0+0

# Get mouse position
pos=($(xdotool getmouselocation | sed -r "s/^x:([[:digit:]]+) y:([[:digit:]]+).*/\1 \2/p"))

# Loop through each screen and compare the offset with the window
# coordinates.
while read name width height xoff yoff
do
  if [ "${pos[0]}" -ge "$xoff" \
    -a "${pos[1]}" -ge "$yoff" \
    -a "${pos[0]}" -lt "$(($xoff+$width))" \
    -a "${pos[1]}" -lt "$(($yoff+$height))" ]
  then
      monitor=$name
      screenw=$width
      screenh=$height
  fi
done < <(xrandr | grep -w connected |
  sed -r "s/^([^ ]*).*\b([-0-9]+)x([-0-9]+)$OFFSET_RE.*$/\1 \2 \3 \4 \5/" |
  sort -nk4,5)

# If we found a monitor, echo it out, otherwise print an error.
if [ ! -z "$monitor" ]
then
    # found monitor
    echo $screenw $screenh
    exit 0
else
    # could not find monitor
    exit 1
fi

在python中,我有以下代码:

^{pr2}$

您可能可以在python中执行我在shell中所做的一些事情,而只直接从python调用xdotool和{}。在

相关问题 更多 >