英文:
Want To Display Random File From Folder With Kivy (Python)
问题
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"
英文:
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"`
答案1
得分: 0
你没有写正确的路径。你只是输入了文件名。将这一行更改为:
self.flagfile = path + "\\" + random.choice(files)
你不需要这样做,但这更有效率:你可以像这样更改路径:path = os.path.join(os.path.expanduser('~'), 'Downloads', 'App', 'w320')
英文:
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')
答案2
得分: 0
你只需要连接文件路径,os.listdir
返回一个文件名列表(不包括完整路径)。
def source_getter(self):
path = "/Users/samarth/Downloads/App/w320"
files = os.listdir(path)
self.flagfile = path + '/' + random.choice(files)
希望这有所帮助。
英文:
you need only to concatenate the file path, os.listdir
return a list of file names (without the complete path).
def source_getter(self):
path = "/Users/samarth/Downloads/App/w320"
files = os.listdir(path)
self.flagfile= path + '/' + random.choice(files)
I hope this helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论