Skip to main content

A Python library to make simple UI using pysdl2

Project description


A library to make simple UI using pysdl2.


class MyApp(sdl2ui.App):
width = 256
height = 224
zoom = 3
# NOTE: the fps you desire: less fps = less CPU usage
fps = 30
name = "My Application"
# NOTE: order the handlers in what you want to display first
default_handlers = [MainHandler, ListSelectorHandler, MenuHandler]
default_resources = [('background', 'background.png')]


Each handler will have three main methods: `init()`, `peek()` and `draw()`:

* `init()` is called as soon as the application is started (instantiated). You
can set all kind of variables in your instance here before anything shows up.
* `peek()` is called at every frame. The capture of the input can be made here.
If peek() returns True, sdl2ui will re-draw the whole screen (using all the
active handlers.
* `render()` is called every time the application need to re-draw the screen.

### Examples

class MainHandler(sdl2ui.Handler):
def peek(self):
if[sdl2.SDL_SCANCODE_D]:[sdl2.SDL_SCANCODE_D] = False
# NOTE: toggle the debugger mode[sdl2ui.handler.DebuggerHandler].toggle()
elif[sdl2.SDL_SCANCODE_S]:[sdl2.SDL_SCANCODE_S] = False
# NOTE: toggle the display of the menu and the list selector[ListSelectorHandler].toggle()[MenuHandler].toggle()
elif[sdl2.SDL_SCANCODE_Q]:[sdl2.SDL_SCANCODE_Q] = False = True
return False
return True

def render(self):'background', x=0, y=0)

class MenuHandler(sdl2ui.Handler):
highlight = (0x00, 0x00, 0xff, 0xff)
line_space = 8
default_active = False
menu_actions = [
("Save", lambda x:,
("Load", lambda x: x.load()),
("Quit", lambda x: x.quit()),

def init(self): = 0

def save(self):
# TODO save()

def load(self):
# TODO load()

def quit(self): = True

def peek(self):
if[sdl2.SDL_SCANCODE_UP]:[sdl2.SDL_SCANCODE_UP] = False -= 1
if < 0: = len(self.menu_actions) - 1
elif[sdl2.SDL_SCANCODE_DOWN]:[sdl2.SDL_SCANCODE_DOWN] = False += 1
if >= len(self.menu_actions): = 0
_, func = self.menu_actions[]
return False
return True

def render(self):
border = 10
x, y = border, border
for i, (label, _) in enumerate(self.menu_actions):
if i ==
# NOTE: you can tint all future draw() and write() using the
# context<tuple_rgba>)
with'font-6', x, y, label)
# NOTE: the font-6 is a bitmap font built-in with the library'font-6', x, y, label)
y += self.line_space

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for pysdl2-sdl2ui, version 0.1.14
Filename, size File type Python version Upload date Hashes
Filename, size pysdl2-sdl2ui-0.1.14.tar.gz (10.4 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page