Open In App

How to Change the Name of a Pygame window?

Last Updated : 01 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 a 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
 
# initializing 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:


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

Similar Reads