Reputation: 2021
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?
Upvotes: 23
Views: 36004
Reputation: 29
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.
Upvotes: 0
Reputation: 6726
import os
os.environ['SDL_VIDEO_CENTERED'] = '1' # window at center
Upvotes: 2
Reputation: 110301
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.
Upvotes: 6
Reputation: 5
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
Upvotes: 0
Reputation: 210968
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))
Upvotes: 3
Reputation: 372
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).
Upvotes: 6
Reputation: 141
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
Upvotes: 14
Reputation: 455
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
Upvotes: 9
Reputation: 2021
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.
Upvotes: 12
Reputation: 16058
(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.
Upvotes: 2