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

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

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 = &quot;/Users/samarth/Downloads/App/w320&quot;
        files = os.listdir(path)
        self.flagfile= random.choice(files)
        
    def build(self):
        Window.clearcolor = (1,1,1,1)
        return MyLayout()


    
if __name__ == &quot;__main__&quot;:
    colorApp().run()
   `

Kivy

&lt;MyLayout&gt;    
    BoxLayout:
        orientation: &quot;vertical&quot;
        size: root.width, root.height

        padding: 50
        spacing: 20
        Image:
            id: cardimage
            source: app.flagfile
                
            on_source: print(self.source)
        Button:
            text: &quot;Hello World&quot;`
    

答案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 + &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 返回一个文件名列表(不包括完整路径)。

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 = &quot;/Users/samarth/Downloads/App/w320&quot;
        files = os.listdir(path)
        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:

确定