Fully transparent windows in Pygame?
Asked Answered
D

2

15

Is it possible to get a fully transparent window in Pygame (see the desktop through it)? I've found how to create a window without a frame, but there doesn't seem to be any obvious way to make it transparent.

I'd be willing to tie into system-specific technology/frameworks as long as there are solutions for both Windows and Mac OS X, but I'm not sure which direction to be looking.

The only topic I was able to find recommended using wxPython, which isn't something I can do for this particular project (needs to be Pygame).

Dairying answered 14/2, 2009 at 23:40 Comment(0)
P
11

PyGame uses SDL, which does not support transparent windows. Although at least on Linux making it transparent is done by the window manager, not the application.

Punctual answered 14/2, 2009 at 23:48 Comment(3)
True, generally the WM does provide transparency effects, but applications can also explicitly enable transparency. urxvt is an example of such an application.Knawel
Really urxvt is fake-transparent. It can only blend with your desktop background, not the other windows. So the answer is totally NO.Lawsuit
When I open urxvt on fullscreen with another window behind, it show both background and window behindPileate
G
38

On Windows, you can create a solid background color then set the window's transparency color with the Win32 API function SetLayeredWindowAttributes(). (Called with pywin32)

Code:

import pygame
import win32api
import win32con
import win32gui

pygame.init()
screen = pygame.display.set_mode((800, 600)) # For borderless, use pygame.NOFRAME
done = False
fuchsia = (255, 0, 128)  # Transparency color
dark_red = (139, 0, 0)

# Create layered window
hwnd = pygame.display.get_wm_info()["window"]
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE,
                       win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED)
# Set window transparency color
win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*fuchsia), 0, win32con.LWA_COLORKEY)

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    screen.fill(fuchsia)  # Transparent background
    pygame.draw.rect(screen, dark_red, pygame.Rect(30, 30, 60, 60))
    pygame.display.update()

Result:

Transparent Pygame Window

Explanation:

Any part of the window using your transparency color will be fully transparent. You can view and interact with any desktop icons or programs located behind your game window.

To remove the window border, you can set Pygame's display mode to NOFRAME.

screen = pygame.display.set_mode((800, 600), pygame.NOFRAME)

See Also:

Goosy answered 14/8, 2018 at 15:35 Comment(4)
thanks a lot. can you provide some resources about method you used in this example please? <pdf/site/reddit/etc>Rend
@Rend My resources are linked in the See Also section.Goosy
This was very helpful thanks so much, +1, by the way is there a way to do this or something similar on Linux?Avalos
@whaleshark I don’t know. I’m not familiar with the Linux desktop API.Goosy
P
11

PyGame uses SDL, which does not support transparent windows. Although at least on Linux making it transparent is done by the window manager, not the application.

Punctual answered 14/2, 2009 at 23:48 Comment(3)
True, generally the WM does provide transparency effects, but applications can also explicitly enable transparency. urxvt is an example of such an application.Knawel
Really urxvt is fake-transparent. It can only blend with your desktop background, not the other windows. So the answer is totally NO.Lawsuit
When I open urxvt on fullscreen with another window behind, it show both background and window behindPileate

© 2022 - 2024 — McMap. All rights reserved.