Open In App

How to make a fully transparent window with PyGame ?

Last Updated : 04 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Fully transparent windows with PyGame have their own advantage while building many games and apps via Python. Thus in order to achieve the functionality of the transparent window that allows the user to see behind a running screen.

For example: 

Geek want to design an app, in which they can see the background screen of the other windows. Thus in order to achieve this, he can use PyGame libraries and windows libraries of Python.

Required modules and Libraries:

pip install pygame

pip install pywin32api

pip install win32con

pip install win32gui

Stepwise Implementation:

Step 1: Import all the required files, libraries, and modules.

Python3




import pygame as py
import win32api
import win32con
import win32gui


Step 2: Initialize the PyGame module and libraries. And declare the PyGame window screen. With the mode of the desired window size. And declaring the windows graphical user interface. 

Python3




py.init() 
# initialize the pygame window
window_screen = py.display.set_mode((700, 450))
# for borderless window use pygame.Noframe
# size of the pygame window will be of width 700 and height 450
hwnd = py.display.get_wm_info()["window"]
# Getting information of the current active window
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong(
                       hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED)


Step 3: Set window transparency color and the text to be displayed on the current window of PyGame. In this, we will be also using the “hwnd” variable that we have created in step2. Declare the font color and size for the font.

Python3




win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(255, 0, 128), 0, win32con.LWA_COLORKEY)
# This will set the opacity and transparency color key of a layered window
font = py.font.SysFont("Times New Roman", 54)
# declare the size and font of the text for the window
text = []
# Declaring the array for storing the text
text.append((font.render("Transparent Window", 0, (255, 100, 0)), (20, 10)))  
text.append((font.render("Press Esc to close the window", 0, (255, 100, 100)), (20, 250)))
# Appending the text in the array


Step 4:  Now we will create a function that will display the text we want in our window.

Python3




# Function to display the text
def show_text():  
    for t in text:
      #  For loop for calling every element in the text
        window_screen.blit(t[0], t[1])
         # Blit is for block transfer


Step 5: Main while loop that will run the program and make the PyGame window active till the escape button or the cross button is pressed. Fill the PyGame window with the transparent color declared above. And calling the show_text() function.

Python3




# Main while loop for the program
done = 0
while not done:  
    # Accessing the event if any occurred
    for event in py.event.get():   
        # Checking if quit button is pressed or not
        if event.type == py.QUIT:  
            #  If quit then store true
            done = 1               
        # Checking if the escape button is pressed or not
        if event.type == py.KEYDOWN:   
            # If the escape button  is pressed then store true in the variable
            if event.key == py.K_ESCAPE: 
                done = 1
     # Transparent background
    window_screen.fill((255,0,128)) 
    #  Calling the show_text function
    show_text() 
    #  Checking for the update in the display
    py.display.update()


Output:

Fully transparent window with PyGame.

Fully transparent window with PyGame.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads