【问题标题】:Pygame multiple monitorPygame 多显示器
【发布时间】:2018-03-28 14:56:00
【问题描述】:

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

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

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

我怎样才能做到这一点?

【问题讨论】:

  • 射击,对不起,我没有测试我的代码。请注意,如果它甚至可以与 pygame 一起使用(即带有 sdl1 的 pygame),我将尝试使用 xrandr、grep、sed 制作一些东西,以便为我自己的目的吐出屏幕分辨率。

标签: python pygame


【解决方案1】:

您可以通过 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中,我有以下代码:

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)

您可能可以在 python 中执行我在 shell 中所做的一些操作,而只直接从 python 调用 xdotoolxrandr

【讨论】:

  • 不确定。您可能可以直接通过 x11 api 执行 xdotool 所做的事情(查看 python xlib)。当然,您可以将完整的输出输入 python 并在那里执行 grep/sed 操作。
猜你喜欢
  • 2012-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-19
  • 2018-09-29
相关资源
最近更新 更多