Guest User

Untitled

a guest
Jul 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import glob
  3. import os
  4. import time
  5.  
  6. from kivy.app import App
  7. from kivy.clock import Clock
  8. from kivy.lang import Builder
  9. from kivy.properties import StringProperty, ObjectProperty
  10. from kivy.uix.image import AsyncImage
  11. from kivy.uix.floatlayout import FloatLayout
  12. from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
  13.  
  14. # Define the path to our images ON RPi for KIVYPIE
  15. # $HOME sets the user path.
  16. folder = '{0}/Pictures'.format(os.environ['HOME'])
  17.  
  18. # Set the extenstions you want to display
  19. extensions = ['.png', '.jpg']
  20. e = [i.upper() for i in extensions]
  21. for i in e:
  22. extensions.append(i)
  23.  
  24. # Define the layout in a KV file.
  25. # It's good to get familiar with the format as it makes building Kivy apps
  26. # very quick
  27.  
  28. kv = """
  29. #:kivy 1.0.9
  30. <KivySlideshow>
  31. scrmgr: scrmgr
  32. size_hint: 1,1
  33. ScreenManager:
  34. id: scrmgr
  35. size_hint: 1,1
  36.  
  37. <PhotoScreen>
  38. Image:
  39. source: root.imsource
  40. """
  41.  
  42. # Simple Screen class that displays a single image
  43. class PhotoScreen(Screen):
  44.  
  45. # String property for the image path
  46. imsource = StringProperty(None)
  47.  
  48. def __init__(self, **kwargs):
  49. super(PhotoScreen, self).__init__(**kwargs)
  50.  
  51. # Set the file path
  52. self.imsource = self.name
  53.  
  54. # This is our base screen
  55. class KivySlideshow(FloatLayout):
  56.  
  57. scrmgr = ObjectProperty(None)
  58. templabel = ObjectProperty(None)
  59. temp = StringProperty("Loading data...")
  60.  
  61. def __init__(self, **kwargs):
  62. super(KivySlideshow, self).__init__(**kwargs)
  63.  
  64. Clock.schedule_once(self.finish_init)
  65.  
  66. def finish_init(self, *args)
  67. # Use a fade transition between photos
  68. self.scrmgr.transition = FadeTransition()
  69. self.scrmgr.transition.duration = 1
  70.  
  71. # Get a list of photos to show
  72. self.photos = []
  73. """for ext in extensions:
  74. photos = glob.glob(os.path.join(folder, ext))
  75. self.photos += photos"""
  76. for file in os.listdir(folder):
  77. if file.endswith(tuple(extensions)):
  78. self.photos += glob.glob(os.path.join(folder, file))
  79.  
  80. # Put them in order
  81. self.photos.sort()
  82.  
  83. # Set some variables that we'll use to keep track of the photos
  84.  
  85. # x is the index of the photo to be shown
  86. self.x = 0
  87.  
  88. # old is a reference to the old photo so we can unload it.
  89. self.old = None
  90.  
  91. # We're ready, so load the first photo
  92. self.load_photo()
  93.  
  94. def load_photo(self, *args):
  95.  
  96. # Get the current photo
  97. current = self.photos[self.x]
  98. print('current photo: {0} Time: {1}'.format(str(current), str(time.strftime('%d %b %Y %H:%M'))))
  99.  
  100. # Create a screen with this name
  101. newphoto = PhotoScreen(name=current)
  102.  
  103. # Add it to the screen manager
  104. self.scrmgr.add_widget(newphoto)
  105.  
  106. # Display it
  107. self.scrmgr.current = current
  108.  
  109. # If there's an old photo
  110. if self.old:
  111.  
  112. # unload it
  113. self.scrmgr.remove_widget(self.old)
  114.  
  115. # Create a reference to the photo so we can remove it later
  116. self.old = newphoto
  117.  
  118. self.x = (self.x + 1) % len(self.photos)
  119. Clock.schedule_once(self.load_photo, 2)
  120.  
  121. # Base app class
  122. class KivySlideshowApp(App):
  123. def build(self):
  124.  
  125. # Return an instance of the slideshow
  126. return KivySlideshow()
  127.  
  128. if __name__ == "__main__":
  129. Builder.load_string(kv)
  130. KivySlideshowApp().run()
Add Comment
Please, Sign In to add comment