Skip to content

Instantly share code, notes, and snippets.

@frankgould
Last active August 9, 2019 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankgould/2464c0a3bf4387165d4968f9e849524b to your computer and use it in GitHub Desktop.
Save frankgould/2464c0a3bf4387165d4968f9e849524b to your computer and use it in GitHub Desktop.
This is the test app used to test and report on memory usage. Due to previous versions of Raspbian releases with graphics driver memory leaks, this code has been tested successfully with the latest version 4.19 named Buster and only runs without lockup with screen resolution 800x600. Any higher resolution and fullscreen lock up.
#!/usr/bin/env python3
import glob
import os
#os.environ['KIVY_GRAPHICS'] = 'gles'
#os.environ['KIVY_WINDOW'] = 'sdl2'
from datetime import datetime
import time, sys, signal
import atexit
import time
import gc
import logging
# Start logging
log_file = '/home/fgould/Desktop/screenmanagertest-log-' + datetime.now().strftime('%m-%d-%y') + '.txt'
logging.basicConfig(filename=log_file,level=logging.DEBUG)
logging.info('Screenmanagertest: Logging Started in ' + log_file + ' @ ' + datetime.now().strftime('%m-%d-%y %H:%M:%S'))
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import StringProperty, ObjectProperty
from kivy.uix.image import AsyncImage
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
# Define the path to our images
folder = '/home/fgould/Desktop/SlideShow/pix'
# Set the extensions you want to display
extensions = ["*.png","*.jpg", "*.JPG"]
# Simple Screen class that displays a single image
class PhotoScreen(Screen):
# String property for the image path
imsource = StringProperty(None)
def __init__(self, **kwargs):
super(PhotoScreen, self).__init__(**kwargs)
# Set the file path
self.imsource = self.name
# This is our base screen
class KivySlideshow(FloatLayout):
scrmgr = ObjectProperty(None)
def __init__(self, **kwargs):
super(KivySlideshow, self).__init__(**kwargs)
# Use a fade transition between photos
self.scrmgr.transition = FadeTransition()
self.scrmgr.transition.duration = 1
# Get a list of photos to show
self.photos = []
for ext in extensions:
photos = glob.glob(os.path.join(folder, ext))
self.photos += photos
# Put them in order
self.photos.sort()
# Set some variables that we'll use to keep track of the photos
# x is the index of the photo to be shown
self.x = 0
# old is a reference to the old photo so we can unload it.
self.old = None
# We're ready, so load the first photo
self.load_photo()
def load_photo(self, *args):
# Get the current photo
current = self.photos[self.x]
print ('current photo: ' + str(current) + ' Time: ' + str(time.strftime('%d %b %Y %H:%M')))
logging.info('current photo: ' + str(current) + ' Time: ' + str(time.strftime('%d %b %Y %H:%M')))
tot_m, used_m, free_m = map(int, os.popen('free -t -m').readlines()[-1].split()[1:])
print('Total Free Memory: ' + str(free_m) + ', Used: ' + str(used_m))
logging.info('Total Free Memory: ' + str(free_m) + ', Used: ' + str(used_m))
gc.collect()
# Create a screen with this name
newphoto = PhotoScreen(name=current)
# Add it to the screen manager
self.scrmgr.add_widget(newphoto)
# Display it
self.scrmgr.current = current
# If there's an old photo
if self.old:
# unload it
self.scrmgr.remove_widget(self.old)
# Create a reference to the photo so we can remove it later
self.old = newphoto
self.x = (self.x + 1) % len(self.photos)
Clock.schedule_once(self.load_photo, 5)
# Base app class
class KivySlideshowApp(App):
def build(self):
# Return an instance of the slideshow
return KivySlideshow()
if __name__ == "__main__":
KivySlideshowApp().run()
@frankgould
Copy link
Author

This is the kivyslideshow.kv file used in the app above.

<KivySlideshow>

    scrmgr: scrmgr
    size_hint: 1,1

    ScreenManager:
        id: scrmgr
        size_hint: 1,1

<PhotoScreen>

    Image:
        source: root.imsource

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment