Pygame Display Position
Asked Answered
A

10

23

I need the window position right after I created a pygame window:

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

By default, the window starts at 0,0 - but I also need x,y if the user changes the window position. Any ideas?

Antibaryon answered 9/11, 2010 at 16:19 Comment(2)
You need the x,y coordinates if the user moves the window? Right? Or you want to set your own coordinates when the window is initialized?Potemkin
I don't think this is possible by default in Pygame.Assure
R
14

It have worked for me :

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))

Taken from https://www.pygame.org/wiki/SettingWindowPosition

Radcliffe answered 11/9, 2018 at 20:15 Comment(1)
Doing that before init is very important !Mediocre
A
12

I need x,y coords of the pygame window - either at start or on window move. The last one is nice to have.

I figured out how to center the pygame window at the bottom of the screen:

    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'

Background: I have x,y coords which are screen related and I must convert the screen coords into window-local coords so that I can use them e.g. to display coords inside the pygame window or to discard coords which are outside the pygame window.

With my approach above, I knwo the initial position. But I can only use a single pygame window (because it's always at the same position) and things go wrong if the user moves the window.

Antibaryon answered 11/11, 2010 at 14:58 Comment(0)
W
9

Here is an example code that return all four corner positions:

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
Willin answered 26/3, 2018 at 0:11 Comment(0)
B
6

There is a pygame.display.get_wm_info() call that gets you the Window handler -- from then on, it is using either X11 or Windows API32 to get information from the window through this handler. I didn't find any readily available information on how to do that.

So, just to be clear: there is no ordinary way to do that from within pygame. You have to proceed with another library, possibly using ctypes, after you get the window handler.

On the other hand, if you have to manipulate the window itself, maybe pygame is not the most suitable library for you to use -- you could try PyQt or even GTK+ - they also provide multmedia facilites while being more proper to operate on the level of GUI Windows and other controls

update There are ways to setup an OpenGL backend for pygame graphics, that will allow complete control of the display - including embedding it in another window, as part of a tkinter or Qt application. People that are interested can search a little deeper along those lines.

Breughel answered 9/11, 2010 at 18:20 Comment(0)
A
6

In Pygame 2, you can alternatively import the _sdl2.video to set the window position. Note that this module is experimental and could be changed in future versions.

import pygame
from pygame._sdl2.video import Window

pygame.init()

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

Using environment variables as mentioned in other answers is sufficient for most cases, but the downside is that once you have change the window position it will not work a second time (at least in Pygame 2).

Arabesque answered 3/6, 2020 at 20:23 Comment(0)
G
3

Pygame is based on Simple DirectMedia Layer (SDL). Hence you can set SDL environment variables.

See pygame wiki - SettingWindowPosition:

You can set the position of the window by using SDL environment variables before you initialise pygame. Environment variables can be set with the os.environ dict in python.

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)) 
Groundling answered 13/9, 2020 at 8:21 Comment(0)
P
2

(0,0) remains the upper left corner whether the window is moved or not. If you're trying to make (0,0) stay physically where it was on the screen when the window initialized, I don't think pygame can do that. Try to make your question clearer if you want clearer answers.

Pushed answered 10/11, 2010 at 0:9 Comment(0)
S
0

To accomplish this where you don't know the monitor size of the user, use screeninfo in addition to the pygame and os packages. Screeninfo is OS-agnostic, meaning you can get the resolution of all monitors regardless of a users operating system.

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
Saving answered 25/2, 2021 at 18:33 Comment(0)
C
0
import os
os.environ['SDL_VIDEO_CENTERED'] = '1'  # window at center
Cuda answered 16/6, 2023 at 11:22 Comment(0)
L
0

When you set pygame window size for the first time, pygame creates window in center, but when you set size not for the first time, pygame doesn't change position.

So, you can try this code:

import pygame
pygame.init()

x, y, width, height = 100, 100, 500, 500
size = pygame.display.Info()
size = [size.current_w, size.current_h]

pygame.display.set_mode((size[0] - x * 2, size[1] - y * 2 - 60)) # 60 is size of window's titlebar.
pygame.display.set_mode((width, height))

But, this code will not work if you have other size of titlebar or if pygame will create window not in center.

Ludendorff answered 24/9, 2023 at 12:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.