Home > Blockchain >  How to display all returned photos on a kivy app using python
How to display all returned photos on a kivy app using python

Time:12-06

I am making a project involving looking up images. I decided to use kivy, creating a system where you can input a name of a folder (located in a specific directory) into a search bar, and it will return all images inside said folder tagged '.jpg'. I am stuck with updating the window to display these images, and I can't find anything online which helps.

from kivy.properties import StringProperty, ObjectProperty
from kivy.uix.screenmanager import Screen
from kivymd.app import MDApp
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.uix.image import Image
from kivy.core.window import Window
import glob
import os

os.chdir(r'directory/of/imagefolders')

class functions:
    def lookup(name):
        root = f'imagefolder'
        for root, subdirs, files in os.walk(f'imagefolder'):
            for d in subdirs:
                if d == name:
                    print(d)
                    path = f'imagefolder/' d
                    print(path)
                    return path

    def load_images(f):
        facefile = []
        for images in glob.glob(f   '\*.jpg'):
            facefile.append(images)
            print(facefile)
        return facefile

class MainApp(MDApp):
    def build(self):
        Window.clearcolor = (1,1,1,1)
        layout = GridLayout(cols=2, row_force_default=True, row_default_height=40,
            spacing=10, padding=20)
        self.val = TextInput(text="Enter name")
        submit = Button(text='Submit', on_press=self.submit)
        layout.add_widget(self.val)
        layout.add_widget(submit)
        return layout

    def submit(self,obj):
        print(self.val.text)
        layout2 = GridLayout(cols=2, row_force_default=True, row_default_height=40,
            spacing=10, padding=20)
        name = self.val.text
        f = functions.lookup(name)
        print(f)
        facefile = functions.load_images(f)
        x = len(facefile)
        print(x)
        for i in range(0, x):
            print(facefile[i])
            self.img = Image(source=facefile[i])
            self.img.size_hint_x = 1
            self.img.size_hint_y = 1
            self.img.pos = (200,100)
            self.img.opacity = 1
            layout2.add_widget(self.img)
            return layout2

MainApp().run()

This is what I tried, but the window doesn't update. All images are returned (demonstrated by print(facefile[i])), but nothing happens with them. Any help would be massively appreciated

CodePudding user response:

Your submit() method is creating Image widgets and adding them to a newly created layout2, You are returning that new layout2 from that method, but that does not add it to your GUI. Try replacing:

layout2.add_widget(self.img)

with:

self.root.add_widget(self.img)
  • Related