把 pygame window 放在前面

Bring a pygame window to front

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

是的,我已经做了我的研究,但找不到任何有用的东西:因此这个问题。

每次我 运行 程序 pygame window 在其他 windows 下面打开。我希望它基于代码以两种方式运行:将 window 固定在顶部 并将 spawn 固定在顶部但没有固定 .

这是我找到的最简单的解决方案: (它还需要 tkinter 来获取系统屏幕指标)

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