Sulabh Tiwari
Sulabh Tiwari

Reputation: 307

Add filebrowser in Kivy

I have a simple working code which display 2 images, however i want it to display after the file has been browsed.

My code:

from kivy.uix.image import Image
from kivy.uix.floatlayout import FloatLayout 
from kivy.app import App
from kivy.uix.scatter import Scatter

class CanvasApp(App):
    def build(self):
        f = floatlayout()
        s = Scatter()
        s1 = Scatter()
        img_1 = Image(source='img0.jpg',pos=(10,280), size=(300,300))
        img_2 = Image(source='img1.jpg',pos=(350,280), size=(300,300))

        f.add_widget(s)
        s.add_widget(img_1)
        f.add_widget(s1)
        f.add_widget(img_2)
        return f
if __name__ == '_main__':
    CanvasApp().run()

Issues in above code: 1. How to provide path in source using filebrowser, what i know about file browser,

from os.path import sep, expanduser, isdir, dirname
user_path = expanduser('~') + sep + 'Documents'
browser = FileBrowser(select_string='Select',
                      favorites=[(user_path, 'Documents')])

How can i use scatter independently for both images. In above mentioned method i can only use scatter properties on img0.jpg

Upvotes: 1

Views: 5262

Answers (1)

picibucor
picibucor

Reputation: 773

In the original kivy.garden.Filebrowser example the following two imports are not mentioned:

from kivy.garden.filebrowser import FileBrowser
from kivy.utils import platform

Here is a small working example:

from kivy.app import App
from os.path import sep, expanduser, isdir, dirname
from kivy.garden.filebrowser import FileBrowser
from kivy.utils import platform

class TestApp(App):

    def build(self):
        if platform == 'win':
            user_path = dirname(expanduser('~')) + sep + 'Documents'
        else:
            user_path = expanduser('~') + sep + 'Documents'
        browser = FileBrowser(select_string='Select',
                              favorites=[(user_path, 'Documents')])
        browser.bind(
                    on_success=self._fbrowser_success,
                    on_canceled=self._fbrowser_canceled)
        return browser

    def _fbrowser_canceled(self, instance):
        print ('cancelled, Close self.')

    def _fbrowser_success(self, instance):
        print (instance.selection)

TestApp().run()

Upvotes: 6

Related Questions