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

How to detect end of picture download in AsyncImage in 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 下载图片?

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

您可以使用 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()