显示文件夹中的随机文件与Kivy(Python)

huangapple go评论87阅读模式
英文:

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

  1. import kivy
  2. from kivy.app import App
  3. from kivy.uix.widget import Widget
  4. from kivy.properties import ObjectProperty
  5. from kivy.lang import Builder
  6. from kivy.uix.image import Image
  7. import random
  8. import os
  9. from kivy.core.window import Window
  10. import numpy as np
  11. from kivy.properties import StringProperty
  12. from kivy.uix.floatlayout import FloatLayout
  13. class MyLayout(Widget):
  14. pass
  15. class colorApp(App):
  16. flagfile = StringProperty()
  17. def source_getter(self):
  18. path = "/Users/samarth/Downloads/App/w320"
  19. files = os.listdir(path)
  20. self.flagfile = random.choice(files)
  21. def build(self):
  22. Window.clearcolor = (1,1,1,1)
  23. return MyLayout()
  24. if __name__ == "__main__":
  25. colorApp().run()

Kivy

  1. <MyLayout>
  2. BoxLayout:
  3. orientation: "vertical"
  4. size: root.width, root.height
  5. padding: 50
  6. spacing: 20
  7. Image:
  8. id: cardimage
  9. source: app.flagfile
  10. on_source: print(self.source)
  11. Button:
  12. 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

  1. import kivy
  2. from kivy.app import App
  3. from kivy.uix.widget import Widget
  4. from kivy.properties import ObjectProperty
  5. from kivy.lang import Builder
  6. from kivy.uix.image import Image
  7. import random
  8. import os
  9. from kivy.core.window import Window
  10. import numpy as np
  11. from kivy.properties import StringProperty
  12. from kivy.uix.floatlayout import FloatLayout
  13. class MyLayout(Widget):
  14. pass
  15. class colorApp(App):
  16. flagfile = StringProperty()
  17. def source_getter(self):
  18. path = &quot;/Users/samarth/Downloads/App/w320&quot;
  19. files = os.listdir(path)
  20. self.flagfile= random.choice(files)
  21. def build(self):
  22. Window.clearcolor = (1,1,1,1)
  23. return MyLayout()
  24. if __name__ == &quot;__main__&quot;:
  25. colorApp().run()
  26. `

Kivy

  1. &lt;MyLayout&gt;
  2. BoxLayout:
  3. orientation: &quot;vertical&quot;
  4. size: root.width, root.height
  5. padding: 50
  6. spacing: 20
  7. Image:
  8. id: cardimage
  9. source: app.flagfile
  10. on_source: print(self.source)
  11. Button:
  12. text: &quot;Hello World&quot;`

答案1

得分: 0

你没有写正确的路径。你只是输入了文件名。将这一行更改为:

  1. 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:

  1. self.flagfile = random.choice(files)

to

  1. self.flagfile = path + &quot;\\&quot; + 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(&#39;~&#39;), &#39;Downloads&#39;, &#39;App&#39;, &#39;w320&#39;)

答案2

得分: 0

你只需要连接文件路径,os.listdir 返回一个文件名列表(不包括完整路径)。

  1. def source_getter(self):
  2. path = "/Users/samarth/Downloads/App/w320"
  3. files = os.listdir(path)
  4. 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).

  1. def source_getter(self):
  2. path = &quot;/Users/samarth/Downloads/App/w320&quot;
  3. files = os.listdir(path)
  4. self.flagfile= path + &#39;/&#39; + random.choice(files)

I hope this helps.

huangapple
  • 本文由 发表于 2023年7月23日 16:44:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定