Skip to content
Related Articles

Related Articles

Improve Article

How to Change the Name of a Pygame window?

  • Last Updated : 22 Jul, 2021

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('Title of window')

To change the icon of pygame window:

Syntax: pygame.display.set_icon(Icon_name)

Stepwise Implementation:

Step 1: First we import and initialize all imported modules. We use import pygame to import all modules and .init() function to initialize those modules.

import pygame
pygame.init() 

Step 2: Initialize a window to display. We use .set_mode() function to create a window. We pass the width and height of our window as parameters to set_mode() function.



pygame.display.set_mode((width_of_window,height_of_window))

Step 3: To change default title and icon of pygame window we use .set_caption() and .set_icon() functions. To change icon first we load icon image using pygame.image.load(“image_path”) function, and then we use .set_icon() to change default image.

pygame.display.set_caption('GeeksforGeeks')


Icon = pygame.image.load('gfglogo.png')


pygame.display.set_icon(Icon)

Step 4: Keep that window running until the user presses the exit button. We use a variable that is true unless the user presses the quit button. To keep the game running we use while loop and check our variable if it is true or not.

running  = True

while running:  
    for event in pygame.event.get():  
        if event.type == pygame.QUIT:  
           running = False

Complete Code:

Python3




# import pygame module
import pygame
 
# initiallizing imported module
pygame.init()
 
# Displaying a window of height
# 500 and width 400
pygame.display.set_mode((400, 500))
 
# Here we set name or title of our
# pygame window
pygame.display.set_caption('GeeksforGeeks')
 
# Here we load the image we want to
# use
Icon = pygame.image.load('gfglogo.png')
 
# We use set_icon to set new icon
pygame.display.set_icon(Icon)
 
# Creating a bool value which checks if
# game is running
running = True
 
# Keep game running till running is true
while running:
     
    # Check for event if user has pushed
    # any event in queue
    for event in pygame.event.get():
         
        # If event is of type quit then set
        # running bool to false
        if event.type == pygame.QUIT:
            running = False

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :