D
D
Denis Mustafin2019-11-18 19:37:20
Python
Denis Mustafin, 2019-11-18 19:37:20

How to open a photo in kivy using filechooser?

There is no way to change the value of self.image.source to the path for the file. Various errors are thrown
Here is the code itself

class SaveLoad(FloatLayout):
    save = ObjectProperty(None)
    image = ObjectProperty(None)
    cancel = ObjectProperty(None)

class LoadDialog(FloatLayout):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)

class Root(FloatLayout):
    loadfile = ObjectProperty(None)
    image = ObjectProperty(None)

    def load(self, path, file):
        # та самая проблемная строка
        path = os.path.join(path, file[0])
        self.image.source = path
        dismiss_popup()

    def dismiss_popup(self):
        self._popup.dismiss()

    def openFile(self):
        if camera == 0:
            content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
            self._popup = Popup(title="Open File", content=content, size_hint=(0.9, 0.9))
            self._popup.open()
        else :
            print("Error")

in kv file
Root:
    BoxLayout:
        orientation: 'vertical'
        size_hint: (1, 1)
        GridLayout:
            cols: 1
            size_hint: (1, .2)
            Button:
                text: "Open\nfile"
                on_press: root.openFile()
        BoxLayout:
            orientation: "horizontal"
            padding: (190, 27)
            Image:
                id: image
                source: 'image/white-background.jpg'
                size_hint: (None, None)
                size: (300 ,300)
# Окно с файловым менеджером
<LoadDialog>:
    BoxLayout:
        orientation: 'vertical'
        size: root.size
        pos: root.pos
        FileChooserIconView:
            id: filechooser
            path: '~/'
        BoxLayout:
            orientation: 'horizontal'
            size_hint_y: None
            height: 40
            Button:
                text: "Close"
                on_press: root.cancel()
            Button:
                text: "Load File"
                on_press: root.load(filechooser.path, filechooser.selection)

Throws this error
File "/home/karacb/.local/lib/python3.7/site-packages/kivy/lang/builder.py", line 64, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "/home/karacb/Desktop/Python/kivy language/Informatik/findface.kv", line 63, in <module>
     on_press: root.load(filechooser.path, filechooser.selection)
   File "/home/karacb/Desktop/Python/kivy language/Informatik/indexCopy.py", line 74, in load
     self.image.source = path
 AttributeError: 'NoneType' object has no attribute 'source'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Mustafin, 2020-01-07
@DenisCompany

Answer. I just had to add ids

def load(self, path, file):
        # та самая проблемная строка
        path = os.path.join(path, file[0])
        self.ids.image.source = path
        dismiss_popup()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question