1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Pygame Window Flickers / Temporarily Closes When Left-Clicked [duplicate]

Discussão em 'Python' iniciado por Stack, Abril 11, 2025.

  1. Stack

    Stack Membro Participativo

    Main Issue


    Every time I left-click on the Pygame window, it appears to flicker, momentarily disappear, or exit and reopen instantly. No crash is reported, and this only happens with left-click, not middle or right click.

    Here is the code I used to troubleshoot this problem:

    import pygame
    from sys import exit

    pygame.init()
    screen = pygame.display.set_mode((400,400))
    pygame.display.set_caption('Flicker Test')
    clock = pygame.time.Clock()

    running = True
    while running:
    for event in pygame.event.get():
    print(event) # Logs every event Pygame sees

    if event.type == pygame.QUIT:
    print("Received QUIT event")
    running = False

    if event.type == pygame.MOUSEBUTTONDOWN:
    print(f"Mouse button {event.button} clicked at {event.pos}")

    pygame.display.update()
    clock.tick(60)

    exit()


    There are no pygame.QUIT events being triggered when this happens. Instead, the console logs only show:

    <Event(1025-MouseButtonDown {'pos': (243, 136), 'button': 1, 'touch': False, 'window': None})>

    Mouse button 1 clicked at (243, 136)

    <Event(32770-VideoExpose {})>

    <Event(32776-WindowExposed {'window': None})>

    <Event(1026-MouseButtonUp {'pos': (243, 136), 'button': 1, 'touch': False, 'window': None})>


    Which suggest that the window loses and regains focus, as opposed to crashing, possibly due to a system-level behavior.

    What I've Tried So Far (None of These Worked):


    1.) Verified It’s Not a Crash

    • Wrapped the game loop in a try/except with traceback.print_exc() and input("Press Enter to close...")

    2.) Ensured I'm Not Clicking the Red "X"

    • Confirmed that I am actually clicking inside the window, not on some layering issue of the close button
    • No QUIT event is triggered when the flicker happens.

    3.) Ran From Standalone Terminal (CMD)

    • Ran the script directly using "python main.py" in Command Prompt.
    • Flicker still happens on left-click, so not an IDE issue.

    4.) Tried Hardware-Accelerated and Buffered Display Mode

    screen = pygame.display.set_mode((400, 400), pygame.HWSURFACE | pygame.DOUBLEBUF)


    5.) Reduced/Increased Frame Rate

    • Only thing of note is the flickering happens slightly less consistently on uncapped frame rates, and significantly worse/more consistent on lower rates.

    6.) Disabled Fullscreen Optimization

    • Right-clicked python.exe -> Properties -> Compatibility tab -> Disabled fullscreen optimizations

    7.) Set SDL Video Driver to Windib

    import os
    os.environ["SDL_VIDEODRIVER"] = "windib"


    8.) Removed Window Decorations

    screen = pygame.display.set_mode((800, 400), pygame.NOFRAME)


    9.) Updated GPU Drivers

    10.) Used Pygame Community Edition (pygame-ce)

    System Info


    Laptop

    OS: Windows 11 Home 24H2

    Python version: 3.10.6

    Pygame-ce version: 2.5.3

    Display resolution: 1920x1080

    GPU: GeForce RTX 4060 Laptop

    Monitor: Internal

    CPU: 13th Gen Intel(R) Core(TM) i7-13620H 2.40 GHz

    TL;DR


    Pygame window momentarily flickers or vanishes when left-clicking anywhere inside the window (not on the close button). No crash, no QUIT event, just a weird focus loss and regain cycle. Happens consistently on a Windows laptop. Tried literally everything from buffer modes to video drivers, DPI overrides, pygame-ce, NOFRAME, and SDL_VIDEODRIVER. No fix yet.

    Continue reading...

Compartilhe esta Página