当前位置: 动力学知识库 > 问答 > 编程问答 >

python - 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

网友答案:

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()
分享给朋友:
您可能感兴趣的文章:
随机阅读: