kivy如何在python中使用在线图像

时间:2018-02-10 15:00:01

标签: python kivy

我试图制作一个使用网址图片的按钮。

icon= str('https://cdn2.iconfinder.com/data/icons/flat-ui-icons-24-px/24/eye-24-256.png')

self.add_widget(ImageButton(source=(icon), size=(100,100), size_hint=(0.1, 0.1), on_press=callback, pos_hint={"x":0.90, "top":1.0}))

class ImageButton(ButtonBehavior, Image):
    pass

出于某种原因显示为白色图像。有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

作为eyllanesc注释,您必须使用AsyncImage子类从服务器异步加载图像。否则,在实例化窗口小部件时,图像将不可用。

另一方面,您在评论中显示的代码;

icon = AsyncImage(source='https://.../icon.png')
self.add_widget(ImageButton(source=(str(icon)))

也不正确,您正试图向source(StringPropery)传递AsyncImage的实例。对此的简单解决方案是继承AsyncImage而不是Image

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

from kivy.core.window import Window
Window.clearcolor = (1, 1, 1, 1)


class ImageButton(ButtonBehavior, AsyncImage):
    pass


class MainWindow(BoxLayout):
    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)
        icon = 'https://cdn2.iconfinder.com/data/icons/flat-ui-icons-24-px/24/eye-24-256.png'
        self.add_widget(ImageButton(source=icon))


class Test(App):
    def build(self):
        return MainWindow()


if __name__ == "__main__":
    Test().run()

enter image description here