Pygame显示位置

时间:2010-11-09 16:19:03

标签: python pygame

我在创建pygame窗口后立即需要窗口位置:

window = pygame.display.set_mode((width, height), 0, 32)
pygame.init()

默认情况下,窗口从0,0开始 - 但如果用户更改窗口位置,我还需要x,y。有什么想法吗?

8 个答案:

答案 0 :(得分:5)

有一个pygame.display.get_wm_info()调用可以获取Window处理程序 - 从那时起,它使用X11或Windows API32通过此处理程序从窗口获取信息。我没有找到任何关于如何做到的信息。

所以 - 只是要清楚:在pygame中没有普通的方法可以做到这一点。在获得窗口处理程序后,你必须使用另一个库,可能使用ctypes。

另一方面,如果您必须操纵窗口本身,也许pygame不是最适合您使用的库 - 您可以尝试PyQt甚至GTK + - 它们还提供多媒体设施,同时更适合操作在GUI窗口和其他控件的级别上。

答案 1 :(得分:5)

我需要pygame窗口的x,y坐标 - 无论是在开始还是在窗口移动。最后一个很好。

我想出了如何将pygame窗口置于屏幕底部:

    pos_x = screen_width / 2 - window_width / 2
    pos_y = screen_height - window_height
    os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (pos_x,pos_y)
    os.environ['SDL_VIDEO_CENTERED'] = '0'

背景:我有与屏幕相关的x,y坐标,我必须将屏幕坐标转换为窗口局部坐标,以便我可以使用它们,例如在pygame窗口内显示coords或丢弃pygame窗口之外的coords。

通过上述方法,我了解了最初的立场。但是我只能使用一个pygame窗口(因为它总是在同一个位置),如果用户移动窗口就会出错。

答案 2 :(得分:2)

无论窗口是否移动,

(0,0)仍然是左上角。如果你试图让(0,0)在窗口初始化时保持在屏幕上的物理位置,我不认为pygame可以做到这一点。如果你想要更清楚的答案,尽量让你的问题更清楚。

答案 3 :(得分:2)

对我有用:

import os
import pygame
x = 100
y = 45
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
pygame.init()
screen = pygame.display.set_mode((100,100))

取自https://www.pygame.org/wiki/SettingWindowPosition

答案 4 :(得分:2)

Pygame基于Simple DirectMedia Layer (SDL)。因此,您可以设置SDL环境变量。

请参见pygame wiki - SettingWindowPosition

在初始化pygame之前,可以使用SDL环境变量来设置窗口的位置。可以使用python中的os.environ dict设置环境变量。

x = 100
y = 0
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)

import pygame
pygame.init()
screen = pygame.display.set_mode((100,100)) 

答案 5 :(得分:1)

以下是返回所有四个角位置的示例代码:

from ctypes import POINTER, WINFUNCTYPE, windll
from ctypes.wintypes import BOOL, HWND, RECT


# get our window ID:
hwnd = pygame.display.get_wm_info()["window"]

# Jump through all the ctypes hoops:
prototype = WINFUNCTYPE(BOOL, HWND, POINTER(RECT))
paramflags = (1, "hwnd"), (2, "lprect")

GetWindowRect = prototype(("GetWindowRect", windll.user32), paramflags)

# finally get our data!
rect = GetWindowRect(hwnd)
print "top, left, bottom, right: ", rect.top, rect.left, rect.bottom, rect.right

# bottom, top, left, right:  644 98 124 644

答案 6 :(得分:1)

在Pygame 2中,您也可以导入_sdl2.video来设置窗口位置。请注意,此模块是实验性的,可能会在以后的版本中更改。

import pygame
from pygame._sdl2.video import Window

pygame.init()

window = Window.from_display_module()
window.position = your_position_tuple

在大多数情况下,使用其他答案中提到的环境变量就足够了,但是缺点是,一旦更改了窗口位置,它就不会再次起作用(至少在Pygame 2中)。

答案 7 :(得分:0)

要在您不知道用户监视器大小的情况下完成此操作,请在 pygame 和 os 包之外使用 screeninfo。 Screeninfo 与操作系统无关,这意味着无论用户的操作系统如何,您都可以获得所有显示器的分辨率。

import pygame
from screeninfo import get_monitors
import os

# Set the size of the pygame window
window_width = 512
window_height = 288
window_size = (window_width, window_height)

# Get the bounds of the users monitors, and select the first one
monitors = get_monitors() # Get the resolution of all of the users monitors
screen_width = monitors[0].width # Get width of first monitor found
screen_height = monitors[0].height # Get height of first monitor found

# Set the x and y coordinates of the pygame window in relation to the monitor's resolution 
# (I wanted my pygame window to be located in the bottom-right of the monitor)
pos_x = screen_width - window_width # Calculate the x-location
pos_y = screen_height - window_height # Calculate the y-location
os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (pos_x,pos_y) # Set pygame window location

pygame.init() # Initialize the pygame window
self.screen = pygame.display.set_mode(size) # Set the location of the pygame window