kivy按钮异步图像

时间:2018-02-22 21:45:09

标签: python kivy

所以我的问题是我想要一个使用URL中图像的按钮。但是图像没有显示在正确的位置,但按钮却显示。图像不可点击但按钮是。我希望图像在按钮上并且可以点击并将我发送到以下屏幕(ProfileScreen)

Kv档案:

:=

Python文件:

<Main_app>:
name: "Main_app"
canvas:
    Color:
        rgb: 0.216, 0.569, 0.639
    Rectangle:
        pos: self.pos
        size: self.size
Label:
    text: ""
    color: 0,0,0,1
    canvas.before:
        Color:
            rgba: 0.251, 0.655, 0.737,1
        Rectangle:
            pos: self.pos
            size: self.size
    pos_hint: {"left":1, "top":1}
    size_hint: 1, 0.0995

Label:
    text: ""
    color: 0,0,0,1
    canvas.before:
        Color:
            rgba: 0.173, 0.451, 0.51,1
        Rectangle:
            pos: self.pos
            size: self.size
    pos_hint: {"left":1, "top":0.9}
    size_hint: 1, 0.01

Label:
    text: ""
    color: 0,0,0,1
    canvas.before:
        Color:
            rgba: 0.251, 0.655, 0.737,1
        Rectangle:
            pos: self.pos
            size: self.size
    pos_hint: {"left":1, "top":0.12}
    size_hint: 1, 0.25

Label:
    text: ""
    color: 0,0,0,1
    canvas.before:
        Color:
            rgba: 0.173, 0.451, 0.51,1
        Rectangle:
            pos: self.pos
            size: self.size
    pos_hint: {"left":1, "top":0.13}
    size_hint: 1, 0.01

    Button:
        size_hint: 0.1, 0.1
        size: 100,100
        pos_hint: {"x":0.90, "top":1.0}
        on_press: app.root.current = "OpeningScreen"
        AsyncImage:
            source: 'https://cdn2.iconfinder.com/data/icons/flat-ui-icons-24-px/24/eye-24-256.png'
            keep_ratio: True
            pos_hint: {"x":0.90, "top":1.0}


<ProfileScreen>:
    name: "ProfileScreen"
    canvas:
        Color:
            rgb: 0.216, 0.569, 0.639
        Rectangle:
            pos: self.pos
            size: self.size

以下是一些它们的样子。 http://prntscr.com/iihmcc

1 个答案:

答案 0 :(得分:0)

您可以创建一个实现Image和ButtonBehavior的新窗口小部件类型。我有同样的问题,发现在某个地方。这是我用来创建新小部件的代码。它不适用于.kv文件。

首先,通过扩展Image和ButtonBehavior类来创建一个新类:

class ImageButton(ButtonBehavior, Image):
    def on_press(self):
        print("on_press")

然后,您可以使用以下函数创建刚刚创建的类的新实例:

@staticmethod
def create_image_button_widget(file_name):
    """
    Function used to create an ImageButton widget.
    :param file_name: Filename of the image to be used as source
    :return: The ImageButton object.
    """
    image_to_add = ImageButton()
    image_to_add.source = "source_of_image.jpg"
    image_to_add.name = file_name
    return image_to_add