PiTFT PyGame Tips

Since the PiTFT screen is fairly small, you may need to write custom UI programs. Pygame is the easiest way by far to do this.

Jeremy Blythe has an excellent tutorial here on getting started. 

However, before you follow that link you'll want to set up pygame for the best compatibility:

Install pip & pygame

Install Pip: sudo apt-get install python-pip

Install Pygame: sudo apt-get install python-pygame

(this will take a while)

Ensure you are running SDL 1.2

SDL 2.x and SDL 1.2.15-10 have some serious incompatibilities with touchscreen. You can force SDL 1.2 by running a script. (Thanks to heine in the forums!)

Edit a new file with sudo nano installsdl.sh
and paste in the following text:

#!/bin/bash

# enable wheezy package sources
echo "deb http://archive.raspbian.org/raspbian wheezy main
" > /etc/apt/sources.list.d/wheezy.list

# set stable as default package source (currently stretch)
echo "APT::Default-release \"stable\";
" > /etc/apt/apt.conf.d/10defaultRelease

# set the priority for libsdl from wheezy higher then the stretch package
echo "Package: libsdl1.2debian
Pin: release n=stretch
Pin-Priority: -10
Package: libsdl1.2debian
Pin: release n=wheezy
Pin-Priority: 900
" > /etc/apt/preferences.d/libsdl

# install
apt-get update
apt-get -y --allow-downgrades install libsdl1.2debian/wheezy

run

sudo chmod +x installsdl.sh

sudo ./installsdl.sh

it will force install SDL 1.2

OK now you can continue with pygame

Using the Capacitive touch screen in PyGame

The 2.8" Capacitive touch screen driver may not work by default in pygame, but this handy script shows how you can capture the device messages in python to create a UI

https://github.com/PrzemoF/pitft_touchscreen

This guide was first published on Apr 29, 2015. It was last updated on Oct 22, 2018. This page (PiTFT PyGame Tips) was last updated on Aug 08, 2018.