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.



Similar Reads

Adding Collisions Using pygame.Rect.colliderect in Pygame
Prerequisite: Drawing shapes in Pygame, Introduction to pygame In this article, we are going to use pygame.Rect.colliderect for adding collision in a shape using Pygame in Python. We can easily add collisions in Pygame shapes using the colliderect( ). For this, we are going to draw two rectangles then we will check if the rectangles are colliding o
3 min read
How to make a PyGame Window?
PyGame is a free and open-source cross-platform library for game development in Python. It was officially written by Pete Shinners to replace PySDL it is suitable for the creation of client-side applications and acts as standalone executables. In this article, we are going to see how to make Pygame window in Python. Command to install pygame on Win
2 min read
PyQt5 - How to create semi transparent window ?
When we design an application in PyQt5, the main window is used to appear by default the window is opaque, but we can make it transparent as well. we can do this by using setWindowOpacity() method which belongs to the QWidget class. Syntax : setWindowOpacity(0.5) Argument : It takes float value as argument : 0 for completely transparent and 1 for o
1 min read
Transparent window in Tkinter
Prerequisite: Python GUI – tkinter Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. To create a transparent window, we will use the attributes() method. Syntax: root.attribut
1 min read
Python | Display text to PyGame window
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of the developer, what type of game he/she wants to develop using this toolkit.. Command to install pygame on wind
6 min read
Python | Drawing different shapes on PyGame window
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of developer, what type of game he/she wants to develop using this toolkit. Command to install pygame : pip instal
3 min read
How to create an empty PyGame window?
Pygame window is a simple window like any other window, in which we display our game screen. It is the first task we do so that we can display our output onto something. Our main goal here is to create a window and keep it running unless the user wants to quit. To perform these tasks first we need to install pygame package and import some pre-defin
2 min read
Allowing resizing window in PyGame
In this article, we will learn How to allow resizing a PyGame Window. Game programming is very rewarding nowadays and it can also be used in advertising and as a teaching tool too. Game development includes mathematics, logic, physics, AI, and much more and it can be amazingly fun. In python, game programming is done in pygame and it is one of the
2 min read
How to get the size of PyGame Window?
In this article, we will learn How to get the size of a PyGame Window. Game programming is very rewarding nowadays and it can also be used in advertising and as a teaching tool too. Game development includes mathematics, logic, physics, AI, and much more and it can be amazingly fun. In python, game programming is done in pygame and it is one of the
2 min read
How to Change the Name of a Pygame window?
PyGame window is a simple window that displays our game on the window screen. By default, pygame uses "Pygame window" as its title and pygame icon as its logo for pygame window. We can use set_caption() function to change the name and set_icon() to set icon of our window. To change the name of pygame window: Syntax: pygame.display.set_caption('Titl
2 min read
Article Tags :
Practice Tags :