How to Change the Name of a Pygame window?
Last Updated :
01 Oct, 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 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
pygame.init()
pygame.display.set_mode(( 400 , 500 ))
pygame.display.set_caption( 'GeeksforGeeks' )
Icon = pygame.image.load( 'gfglogo.png' )
pygame.display.set_icon(Icon)
running = True
while running:
for event in pygame.event.get():
if event. type = = pygame.QUIT:
running = False
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...