Make a pygame resizable window that can be snapped to the screen

Viewed 315

I've created a simple screen with:

user_display = pg.display.Info()

win = pg.display.set_mode((user_display.current_w, user_display.current_h), pg.RESIZABLE)

This creates a window that can be resized, which sets itself to the user's current screen size. The problem with this is that the window's location appears off to the corner. Ideally, the desired effect would be what happens when the fullscreen icon on the top of the window was clicked, by default. Is there a way to set this so that the screen is automatically snapped to the top?

2 Answers

I guess you're talking about maximizing the window, not fullscreen mode.

If you only care about Windows, you can use the Win32-API to maximize the window (using the win32gui module):

import pygame
import win32gui
import win32con

def main():
    pygame.init()
    screen = pygame.display.set_mode((800, 600), pygame.RESIZABLE)
    win32gui.ShowWindow(win32gui.GetForegroundWindow(), win32con.SW_MAXIMIZE)

    ball = pygame.Rect(0, 300, 32, 32)
    dir = 1

    while True:
        events = pygame.event.get()

        for e in events:
            if e.type == pygame.QUIT:
                return
            if e.type == pygame.VIDEORESIZE:
                screen = pygame.display.set_mode((e.w,e.h), pygame.RESIZABLE)
                ball = pygame.Rect(0, int(e.h/2), 32, 32)

        if not screen.get_rect().contains(ball):
            dir *= -1
        ball.move_ip(dir, 0)

        screen.fill((30, 30, 30))
        pygame.draw.rect(screen, pygame.Color("dodgerblue"), ball)
        pygame.display.flip()
main()

If you want to maximize the window on other systems, like Linux, I have no idea, but you could wrap your pygame window in a tkinter window and maximze that:

import pygame
import tkinter as tk
import os

def main():
    root = tk.Tk()
    embed = tk.Frame(root, width = 800, height = 600)
    embed.pack(fill="both", expand=True)
    os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
    os.environ['SDL_VIDEODRIVER'] = 'windib'
    root.state('zoomed')
    screen = pygame.display.set_mode((800, 600), pygame.RESIZABLE)

    ball = pygame.Rect(0, 300, 32, 32)
    dir = 1

    while True:
        events = pygame.event.get()

        for e in events:
            if e.type == pygame.QUIT:
                return
            if e.type == pygame.VIDEORESIZE:
                screen = pygame.display.set_mode((e.w,e.h), pygame.RESIZABLE)
                ball = pygame.Rect(0, int(e.h/2), 32, 32)

        if not screen.get_rect().contains(ball):
            dir *= -1
        ball.move_ip(dir, 0)

        screen.fill((30, 30, 30))
        pygame.draw.rect(screen, pygame.Color("dodgerblue"), ball)
        pygame.display.flip()
        root.update()      
main()

The position of the window can be set by setting the environment variable SDL_VIDEO_WINDOW_POS before initializing a window by pygame.display.set_mode(). For instance

import os
import pygame

os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(20, 20)

pygame.init()
user_display = pg.display.Info()
win = pg.display.set_mode((user_display.current_w, user_display.current_h), pg.RESIZABLE)

See also SettingWindowPosition


If you want to change the position of the window in-program, then you have to set the position first and recreate the display surface by pygame.display.set_mode.
See How to move a no frame pygame windows when user click on it?.

Related