1
from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame  # import after disabling prompt

screen = pygame.display.set_mode((800, 800))
screen.fill((50, 50, 50))  # Dark gray color
pygame.display.update()

Yes, I did my research already, and couldn't find anything helpful: hence this question.

Every time I run the program the pygame window opens below other windows. I want it to behave in 2 ways based on code: Pin the window on top and spawn on top but no pin.

martineau
  • 119,623
  • 25
  • 170
  • 301
Ishan Jindal
  • 198
  • 1
  • 2
  • 18

1 Answers1

1

Here is the simplest solution I found: (It also requires tkinter to get system screen metrics)

from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame  # import after disabling environ prompt
from win32gui import SetWindowPos
import tkinter as tk


root = tk.Tk()  # create only one instance for Tk()
root.withdraw()  # keep the root window from appearing

screen_w, screen_h = root.winfo_screenwidth(), root.winfo_screenheight()
win_w = 250
win_h = 300

x = round((screen_w - win_w) / 2)
y = round((screen_h - win_h) / 2 * 0.8)  # 80 % of the actual height

# pygame screen parameter for further use in code
screen = pygame.display.set_mode((win_w, win_h))

# Set window position center-screen and on top of other windows
# Here 2nd parameter (-1) is essential for putting window on top
SetWindowPos(pygame.display.get_wm_info()['window'], -1, x, y, 0, 0, 1)

# regular pygame loop
done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            done = True
Ishan Jindal
  • 198
  • 1
  • 2
  • 18
  • You don't even need `tkinter`, you can just use `pygame.display.Info().current_w` and `pygame.display.Info().curent_h` to ge the screen size. – D_00 Dec 19 '21 at 11:41