How to run pygame on Raspberry TFT (ideally without startx)?

Creating a bug report/issue

I would like to run a pygame app in kiosk mode, e.g. immediately after boot without needing startx. I tried several ways and drivers, but every time I start a test script I hear a “click”, no image, and the current screen resolution is reported as “1024x768” instead of 800x480, so I assume pygame goes to the HDMI output instead.

Required Information

  • DietPi version | cat /boot/dietpi/.version
    G_DIETPI_VERSION_CORE=9
    G_DIETPI_VERSION_SUB=1
    G_DIETPI_VERSION_RC=1
    G_GITBRANCH=‘master’
    G_GITOWNER=‘MichaIng’
    G_LIVE_PATCH_STATUS[0]=‘not applicable’
  • Distro version | echo $G_DISTRO_NAME $G_RASPBIAN
    bullseye 0
  • Kernel version | uname -a
    6.1.21-v8+ #1642 SMP PREEMPT Mon Apr 3 17:24:16 BST 2023 aarch64 GNU/Linux
  • Architecture | dpkg --print-architecture
    arm64
  • SBC model | echo $G_HW_MODEL_NAME or (EG: RPi3)
    RPI3
  • Power supply used | (EG: 5V 1A RAVpower)
    original Pi
  • SD card used | (EG: SanDisk ultra)
    SanDisk ultra

Additional Information (if applicable)

import pygame
import os

os.putenv(‘SDL_VIDEODRIVER’, ‘kmsdrm’)
os.putenv(‘SDL_FBDEV’ , ‘/dev/fb0’)

pygame.init()

If anyone can suggest a good solution to establish a solution with startx involved, I’m all ears, too. It works when I put the command to start my pathon script into .Xsession, but if the script crashes for any reason, the machine is stuck at the login screen. Would rather have it restart my script properly. The whole thing does work when I configure dietpi to boot into Chromium, then run my script as a service. But that seems to be overkill when I don’t need the browser.

Are you sure that /dev/fb0 is the correct framebuffer? Usually the first one (fb0) is the HDMI output.
Pygame itself has also several options to set a resolution and select a display
https://www.pygame.org/docs/ref/display.html#pygame.display.set_mode

Why do I get a 403 error when I try to post here?!?

thanks, but same results with fb0 and fb1

I dug a little further. When I don’t set SDL_VIDEODRIVER variable:

import pygame
import os

#os.environ["SDL_VIDEODRIVER"] = "directfb"
os.environ["SDL_FBDEV"] = "/dev/fb0"

print (os.getenv("SDL_VIDEODRIVER"), os.getenv("SDL_FBDEV"))
pygame.init()
print(pygame.display.get_driver())

then I get

pygame 2.5.2 (SDL 2.28.3, Python 3.9.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
None /dev/fb0
offscreen

When I try and set the varible, the script throws an error:

pygame.error: video system not initialized

Can’t make head nor tails of this.

Your first post got flagged for “typing suspiciously fast” like a bot, you probably copie and pasted the text into the forum?!

Is the TFT working at all, without pygame?

Yes, I did copy/paste the code.

And yes, the tft works. Also, I can run the pygame script when dietpi is booted into Chromium kiosk mode. Steps to reproduce:

  • Boot chromium in Kiosk mode
  • log in via SSH
  • start pygame script

Also works when I run the python program as a service: Chromium starts, then the script takes over.

OK, I found a great solution.

I set the dietpi to run a custom background script on boot with only the command:

sudo X &

That starts a blank X server when the pi boots up. My Python script is started as a service:

[Unit]
Description=CoffeePi Daemon
After=multi-user.target

[Service]
Type=simple
Restart=always
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/dietpi/.Xauthority"
User=dietpi
ExecStart=python3 /home/dietpi/CoffeeDB/Main.py

[Install]
WantedBy=multi-user.target

The Python script starts PyGame, and everything works as intended. If the python app crashes, the service will be restarted automatically.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.