I have this folder full of images and I need to be able to display a random file from the folder each time I run the app, currently I have the file saved under the variable flagfile and don't seem to be getting an error when I run this code. For some reason the image doesn't end up displaying on the actual app though I'm new to kivy and was hoping for help.
My Code so far: Python
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.uix.image import Image
import random
import os
from kivy.core.window import Window
import numpy as np
from kivy.properties import StringProperty
from kivy.uix.floatlayout import FloatLayout
class MyLayout(Widget):
pass
class colorApp(App):
flagfile = StringProperty()
def source_getter(self):
path = "/Users/samarth/Downloads/App/w320"
files = os.listdir(path)
self.flagfile= random.choice(files)
def build(self):
Window.clearcolor = (1,1,1,1)
return MyLayout()
if __name__ == "__main__":
colorApp().run()
`
Kivy
<MyLayout>
BoxLayout:
orientation: "vertical"
size: root.width, root.height
padding: 50
spacing: 20
Image:
id: cardimage
source: app.flagfile
on_source: print(self.source)
Button:
text: "Hello World"`
You didn't write the correct path. You just typed the filename. Change this line:
self.flagfile = random.choice(files)
to
self.flagfile = path + "\\" + random.choice(files)
and you don't need do that but it's more efficient: You can change your path like this: path = os.path.join(os.path.expanduser('~'), 'Downloads', 'App', 'w320')