Pygame Display Position

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?

2

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

1

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.

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

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

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.

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

(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.

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

import os
os.environ['SDL_VIDEO_CENTERED'] = '1'  # window at center

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Pygame Display Position

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?

2

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

1

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.

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

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

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.

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

(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.

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

import os
os.environ['SDL_VIDEO_CENTERED'] = '1'  # window at center

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật