I’m having trouble running a pygame script on my Raspberry Pi Zero 2W with Raspberry Pi OS Lite. The script is intended to display images directly on the screen. However, I’m encountering errors and unable to get it to display anything. Here’s the full issue and the steps I’ve taken so far: Problem:
I have a Python script that uses pygame to show images on the Raspberry Pi's connected display. However, when I try to run it, the display doesn't show anything. And get error messages returned (see below) I’ve tried to use the framebuffer (fbcon) and X Server, but neither seems to work properly.
Steps Taken:
Raspberry Pi OS Lite: I’m using Raspberry Pi OS Lite (no desktop environment).
Installed pygame: I installed pygame and its dependencies (libsdl2).
Framebuffer (fbcon): I tried to set the SDL video driver to fbcon (framebuffer) with:
export SDL_VIDEODRIVER=fbcon export SDL_FBDEV=/dev/fb0
but I encountered the error:
pygame.error: fbcon not available
DirectFB: I tried using directfb as an alternative:
export SDL_VIDEODRIVER=directfb
but this also resulted in:
pygame.error: directfb not available
X Server: I installed lightdm to use an X Server for the graphical interface:
sudo apt install -y lightdm
sudo systemctl enable lightdm
After a reboot, I still see the login prompt on the connected display, and when I try to run the pygame script, nothing shows up on the display. I’ve set DISPLAY=:0, but it seems like the X Server is not properly initialized.
Testing with pygame: I attempted running the test script with the following initialization:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 480)) # Adjust resolution to match your display
pygame.display.set_caption("Framebuffer Test")
screen.fill((0, 128, 255)) # Blue background
pygame.display.flip()
pygame.time.wait(5000)
This script does not display anything on the screen either.
Things I’ve Tried to Resolve:
- Ensured that libsdl2 is properly installed.
- Verified that /dev/fb0 exists.
- Set environment variables like SDL_VIDEODRIVER, SDL_FBDEV, and DISPLAY=:0.
- Tried starting X Server with startx, "but it fails with the error Only console users are allowed to run the X server."
System Information:
Raspberry Pi Zero 2W
Raspberry Pi OS Lite (no GUI)
pygame 2.6.1
Python 3.11.2
Libraries: libsdl2, lightdm, xorg