Created
July 17, 2018 17:56
-
-
Save cruor99/6e14640a67887cd7d9d12e1747f768fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import glob | |
import os | |
import time | |
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 ON RPi for KIVYPIE | |
# $HOME sets the user path. | |
folder = '{0}/Pictures'.format(os.environ['HOME']) | |
# Set the extenstions you want to display | |
extensions = ['.png', '.jpg'] | |
e = [i.upper() for i in extensions] | |
for i in e: | |
extensions.append(i) | |
# Define the layout in a KV file. | |
# It's good to get familiar with the format as it makes building Kivy apps | |
# very quick | |
kv = """ | |
#:kivy 1.0.9 | |
<KivySlideshow> | |
scrmgr: scrmgr | |
size_hint: 1,1 | |
ScreenManager: | |
id: scrmgr | |
size_hint: 1,1 | |
<PhotoScreen> | |
Image: | |
source: root.imsource | |
""" | |
# 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) | |
templabel = ObjectProperty(None) | |
temp = StringProperty("Loading data...") | |
def __init__(self, **kwargs): | |
super(KivySlideshow, self).__init__(**kwargs) | |
Clock.schedule_once(self.finish_init) | |
def finish_init(self, *args) | |
# 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""" | |
for file in os.listdir(folder): | |
if file.endswith(tuple(extensions)): | |
self.photos += glob.glob(os.path.join(folder, file)) | |
# 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: {0} Time: {1}'.format(str(current), str(time.strftime('%d %b %Y %H:%M')))) | |
# 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, 2) | |
# Base app class | |
class KivySlideshowApp(App): | |
def build(self): | |
# Return an instance of the slideshow | |
return KivySlideshow() | |
if __name__ == "__main__": | |
Builder.load_string(kv) | |
KivySlideshowApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment