Python Pygame多监视器
我正在尝试获取应用程序中当前监视器大小的值。 我和GNOME在ubuntu 16.04上。Python Pygame多监视器,python,pygame,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)) 我知道屏幕
问题是我有上网本显示器和外部显示器,所以如果我尝试执行以下操作:
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))
我知道屏幕的宽度是上网本显示器+外部显示器,所以分辨率如下:(32861080) 因此,我的另一个尝试是使用pygame.display.list_modes()获取有关监视器的信息,以获得一些显示分辨率设置,但我得到的列表如下:
[(32861080),(1366768),(1360768),(1024768),(960720), (960, 600), (960, 540), (928, 696), (896, 672), (840, 525), (800, 600), (800, 512), (720, 450), (700, 525), (680, 384), (640, 512), (640480),(576432),(512384),(400300),(320240)] 但仍然不知道当前的“活动”监视器是什么。
如果我在我的上网本显示器上打开我的程序,我希望得到该显示器的分辨率,相反,如果我在外部显示器上打开它,我希望得到该分辨率,而不是一个加上另一个。
如何实现这一点?您可以通过shell使用x11实用程序。我有以下脚本输出活动屏幕的宽度和高度(鼠标光标所在的屏幕)。您可能需要安装
xdool
#!/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调用
xdool
和xrandr
。Shoot,对不起,我没有测试我的代码。请注意,如果pygame(即带有sdl1的pygame)也可以使用,我将尝试使用xrandr、grep、sed制作一些东西,为自己的目的输出屏幕分辨率。不确定。您可能可以直接通过X11API完成xdotool所做的事情(请查看python xlib)。当然,您可以将完整的输出sip到python中,并在那里执行grep/sed操作。
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)