简体   繁体   中英

How to pin a pygame window on top?

I need to make a pygame window stay on top of other windows. I found a way to do so on this discussion:

How to make python window run as "Always On Top"?

But this doesn't work in my python code.

Here is my code:

# Imports
import pygame as pg
from ctypes import windll

SetWindowPos = windll.user32.SetWindowPos

pg.init()
win = pg.display.set_mode((200, 30))

x, y = 100, 100
# Pin Window to the top
SetWindowPos(pygame.display.get_wm_info()['window'], -1, x, y, 0, 0, 0x0001)


#Main Loop
run = True
while run:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            run = False
            break

For ctypes.windll to work, you have to first configure the types of the function's arguments (IIRC you don't have to this if you're on a 32-bit machine).

So your code should look like this:

import pygame
import ctypes
from ctypes import wintypes 

def main():
    pygame.init()
    screen = pygame.display.set_mode((400, 200))
    
    hwnd = pygame.display.get_wm_info()['window']
    
    user32 = ctypes.WinDLL("user32")
    user32.SetWindowPos.restype = wintypes.HWND
    user32.SetWindowPos.argtypes = [wintypes.HWND, wintypes.HWND, wintypes.INT, wintypes.INT, wintypes.INT, wintypes.INT, wintypes.UINT]
    user32.SetWindowPos(hwnd, -1, 600, 300, 0, 0, 0x0001)
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
        screen.fill('grey')
        pygame.display.flip()

if __name__ == '__main__':
    main()

I prefer to use the pywin32 package instead, because the functions just work and the constants you need are available.

import pygame
import win32gui
import win32con

def main():
    pygame.init()
    screen = pygame.display.set_mode((400, 200))

    hwnd = win32gui.GetForegroundWindow()

    win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 600, 300, 0, 0, win32con.SWP_NOSIZE)
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
        screen.fill('grey')
        pygame.display.flip()

if __name__ == '__main__':
    main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM