Skip to main content

toggle_fullscreen — wiki

This is a function I use to toggle between windowed and full screen mode. It restores the content of the screen after toggling. It only works correctly on software surfaces.

import pygame
from pygame.locals import *

def toggle_fullscreen():
    screen = pygame.display.get_surface()
    tmp = screen.convert()
    caption = pygame.display.get_caption()
    cursor = pygame.mouse.get_cursor()  # Duoas 16-04-2007 
    
    w,h = screen.get_width(),screen.get_height()
    flags = screen.get_flags()
    bits = screen.get_bitsize()
    
    pygame.display.quit()
    pygame.display.init()
    
    screen = pygame.display.set_mode((w,h),flags^FULLSCREEN,bits)
    screen.blit(tmp,(0,0))
    pygame.display.set_caption(*caption)

    pygame.key.set_mods(0) #HACK: work-a-round for a SDL bug??

    pygame.mouse.set_cursor( *cursor )  # Duoas 16-04-2007
    
    return screen

if __name__ == '__main__':
    SW,SH = 640,480
    screen = pygame.display.set_mode((SW,SH))
    pygame.display.set_caption('this is a test')
    
    _quit = False
    while not _quit:
        for e in pygame.event.get():
            if (e.type is KEYDOWN and e.key == K_RETURN
                    and (e.mod&(KMOD_LALT|KMOD_RALT)) != 0):
                toggle_fullscreen()
            if e.type is QUIT: _quit = True
            if e.type is KEYDOWN and e.key == K_ESCAPE: _quit = True
            
        screen = pygame.display.get_surface()
        import random
        rr = random.randrange
        screen.fill((rr(0,256),rr(0,256),rr(0,256)),(rr(0,SW),rr(0,SH),32,32))
        pygame.display.flip()

- philhassey & illume

Added two lines to preserve the cursor shape as well. -Duoas

Comments:

I had to comment the "pygame.display.quit()" line for this to work for

Keith Nemitz:

This does not work perfectly under MacOSX. When returning from fullscreen mode, the window loses its title bar. And, if you select the window from the window menu in the menu bar, an invisible window pops up with a title bar, but it is non-active.

Got 'OverflowError: signed integer is less than minimum' on Debian stretch (Python 2.7 / Pygame 1.9). Found that FULLSCREEN value is somehow negative and XOR operation works wrong with it. Looks like each xor just adds this value to flags variable, so it becames negative too. Started to work after changing XOR operation ('^') to ADD ('+'). Replaced this line

screen = pygame.display.set_mode((w,h),flags^FULLSCREEN,bits)
by
screen = pygame.display.set_mode((w,h),flags+FULLSCREEN,bits)