How do I maximize the display screen in PyGame?

Question:

I am new to Python programming and I recently started working with the PyGame module. Here is a simple piece of code to initialize a display screen. My question is : Currently, the maximize button is disabled and I cannot resize the screen. How do I enable it to switch between full screen and back?
Thanks

import pygame, sys
from pygame.locals import *

pygame.init()

#Create a displace surface object
DISPLAYSURF = pygame.display.set_mode((400, 300))

mainLoop = True

while mainLoop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainLoop = False
    pygame.display.update()

pygame.quit()

Answers:

The method you are looking for is pygame.display.toggle_fullscreen

Or, as the guide recommends in most situations, calling pygame.display.set_mode() with the FULLSCREEN tag.

In your case this would look like

DISPLAYSURF = pygame.display.set_mode((400, 300), pygame.FULLSCREEN)

(Please use the pygame.FULLSCREEN instead of just FULLSCREEN because upon testing with my own system FULLSCREEN just maximized the window without fitting the resolution while pygame.FULLSCREEN fit my resolution as well as maximizing.)

Answered By: Zenohm

To become fullscreen at native resolution, do

DISPLAYSURF = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)

To make the window resizeable, add the pygame.RESIZABLE parameter when seting the mode. You can set the mode of the screen surface multiple times but you might have to do pygame.display.quit() followed by pygame.display.init()

You should also check the pygame documentation here http://www.pygame.org/docs/ref/display.html#pygame.display.set_mode

Answered By: muddyfish

You’d have to add the full screen parameter onto the display declaration like this:

import pygame, sys
from pygame.locals import *

pygame.init()

#Create a displace surface object
DISPLAYSURF = pygame.display.set_mode((400, 300), FULLSCREEN)

mainLoop = True

while mainLoop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainLoop = False
    pygame.display.update()

pygame.quit()
Answered By: Michael Chav

This will let you toggle from maximize to the initial size

import pygame, sys
from pygame.locals import *

pygame.init()

#Create a displace surface object
#Below line will let you toggle from maximize to the initial size
DISPLAYSURF = pygame.display.set_mode((400, 300), RESIZABLE)

mainLoop = True

while mainLoop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainLoop = False
    pygame.display.update()

pygame.quit()
Answered By: Tanmaya Meher
import pygame, os

os.environ['SDL_VIDEO_CENTERED'] = '1' # You have to call this before pygame.init()

pygame.init()

info = pygame.display.Info() # You have to call this before pygame.display.set_mode()
screen_width,screen_height = info.current_w,info.current_h

These are the dimensions of your screen/monitor. You can use these or reduce them to exclude borders and title bar:

window_width,window_height = screen_width-10,screen_height-50
window = pygame.display.set_mode((window_width,window_height))
pygame.display.update()
Answered By: Apostolos

Just use pygame.RESIZABLE as an easier option.

Answered By: Shivansh Sharma
screen = pygame.display.set_mode()

or

screen = pygame.display.set_mode(flags=pygame.FULLSCREEN)
Answered By: fedor.chernolutsky
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.