[Python] Python pygame fails to output to /dev/fb1 on a Raspberry Pi + TFT screen

Discussão em 'Python' iniciado por Stack, Junho 5, 2025.

  1. Stack

    Stack Membro Participativo

    TL;DR


    I am fiddling with a Raspberry Pi 2 and a 2.8" TFT touch screen attached to the Pi's GPIO. The Pi is also connected to a HDMI monitor.
    My issue is that my Python3 pygame script is not able to use the TFT screen, but always displays on my HDMI screen instead.

    Some background


    I've installed the latest vanilla Raspbian ready-to-use distro and followed the TFT screen installation steps, everything works well: the TFT can display the console and X without issue. The touchscreen is calibrated and moves the cursor correctly. I can also see a new framebuffer device as /dev/fb1.

    I've tried the following to test this new device:

    sudo fbi -T 2 -d /dev/fb1 -noverbose -a my_picture.jpg


    => This successfully displays the pic on the TFT screen

    while true; do sudo cat /dev/urandom > /dev/fb1; sleep .01; done


    => This successfully displays statics on the TFT screen

    However, when I run this Python3/pygame script, the result appears in the HDMI screen consistently and not on the TFT screen:

    #!/usr/bin/python3

    import os, pygame, time

    def setSDLVariables():
    print("Setting SDL variables...")
    os.environ["SDL_FBDEV"] = "/dev/fb1"
    os.environ["SDL_VIDEODRIVER"] = driver
    print("...done")

    def printSDLVariables():
    print("Checking current env variables...")
    print("SDL_VIDEODRIVER = {0}".format(os.getenv("SDL_VIDEODRIVER")))
    print("SDL_FBDEV = {0}".format(os.getenv("SDL_FBDEV")))

    def runHW5():
    print("Running HW5...")
    try:
    pygame.init()
    except pygame.error:
    print("Driver '{0}' failed!".format(driver))
    size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
    print("Detected screen size: {0}".format(size))
    lcd = pygame.display.set_mode(size)
    lcd.fill((10,50,100))
    pygame.display.update()
    time.sleep(sleepTime)
    print("...done")

    driver = 'fbcon'
    sleepTime= 0.1

    printSDLVariables()
    setSDLVariables()
    printSDLVariables()
    runHW5()


    The script above runs as follow:

    pi@raspberrypi:~/Documents/Python_HW_GUI $ ./hw5-ThorPy-fb1.py
    Checking current env variables...
    SDL_VIDEODRIVER = None
    SDL_FBDEV = None
    Setting SDL variables...
    ...done
    Checking current env variables...
    SDL_VIDEODRIVER = fbcon
    SDL_FBDEV = /dev/fb1
    Running HW5...
    Detected screen size: (1920, 1080)
    ...done


    I have tried different drivers (fbcon, directfb, svgalib...) without success.

    Any help or idea would be greatly appreciated, I've been through a lot of doc, manuals and samples and just ran out of leads :/ Furthermore, it appears that a lot of people have succeeded in getting Python3/pygame to output to their TFT screen via /dev/fb1.

    Continue reading...

Compartilhe esta Página