如何在Kivy中检测AsyncImage中的图片下载结束?

时间:2015-03-31 14:31:38

标签: python kivy

我正在编写一个像这样简单的应用程序:

#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import AsyncImage


class Application(App):

    def build(self):
        box_layout = BoxLayout(orientation='vertical')
        img = AsyncImage(
            source='http://pl.python.org/forum/Smileys/default/cheesy.gif')

        box_layout.add_widget(img)
        return box_layout

    def __on_image_loaded(self):
        print('Very importatn stuff executed afer image has been downloaded by img widget.')

app = Application()
app.run()

如何检测AsyncImage小部件是否已从指定的URL下载图片?

或者我可以自己编写下载线程并使用Image小部件但是在这种情况下我该怎么做才能将原始字节从内存加载到Image小部件以将它们显示为图片?

1 个答案:

答案 0 :(得分:1)

您可以使用img._coreimage.bind(on_load=self.on_image_loaded)

#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import AsyncImage


class Application(App):

    def build(self):
        box_layout = BoxLayout(orientation='vertical')
        img = AsyncImage(source='http://pl.python.org/forum/Smileys/default/cheesy.gif')
        img._coreimage.bind(on_load=self.on_image_loaded)

        box_layout.add_widget(img)
        return box_layout

    def on_image_loaded(self, *args):
        print('Very importatn stuff executed afer image has been downloaded by img widget.')

app = Application()
app.run()