Pygame多重监视器

时间:2018-03-28 14:56:00

标签: python pygame

我试图在我的应用程序中获取当前监视器大小的值。 我在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_modes()获取有关监视器的信息,以获得一些显示分辨率settigns但我得到一个列表如:

  

[(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)]

但仍然不知道现在的活跃"活跃的"监测。
如果我在我的上网本监视器中打开我的程序,我希望得到该监视器的分辨率,相反,如果我在外部打开它,我希望该分辨率不是一个加上其他。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

您可以通过shell使用x11实用程序。我有以下脚本输出活动屏幕宽度和高度(鼠标光标所在的屏幕)。您可能需要安装<JBL@gmail.com>

xdotool

在python中,我有以下代码:

#!/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中执行我在shell中所做的一些操作,而只能直接从python调用res = subprocess.run("./activescreen", stdout=subprocess.PIPE, stderr=subprocess.PIPE) if(res.returncode == 0): wh = res.stdout.split(b' ') screenw = int(wh[0]) screenh = int(wh[1]) screen = pg.display.set_mode((screenw, screenh), pg.RESIZABLE) xdotool

相关问题