Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将两个不同的GIF绑定到kivy中的切换按钮?_Python_Kivy_Animated Gif_Kivy Language - Fatal编程技术网

Python 如何将两个不同的GIF绑定到kivy中的切换按钮?

Python 如何将两个不同的GIF绑定到kivy中的切换按钮?,python,kivy,animated-gif,kivy-language,Python,Kivy,Animated Gif,Kivy Language,我从一个有用的用户那里得到了这段代码,它几乎是完美的 from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.lang import Builder Builder.load_string(""" <ExampleApp>: id: main orientation: "vertical" Button: size_hint_x: None

我从一个有用的用户那里得到了这段代码,它几乎是完美的

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder

Builder.load_string("""
<ExampleApp>:
    id: main
    orientation: "vertical"
    Button:
        size_hint_x: None
        size_hint_y: None
        height: 300
        width: self.height
        center: self.parent.center
        text: ""
        on_press: gif.anim_delay = 0.09
        on_press: gif._coreimage.anim_reset(True)

        Image:
            id: gif
            source: 'power_on.gif'
            center: self.parent.center
            height: 300
            width: self.height
            allow_stretch: True
            anim_delay: -1
            anim_loop: 1


""")

class ExampleApp(App, BoxLayout):
    def build(self):
        return self


if __name__ == "__main__":
    ExampleApp().run()
但那根本不起作用。它甚至从未播放过开机gif,因为它立即将gif的来源更改为关机


正确的方法是什么?

下面的第一个gif是
power\u off.gif

-考虑添加
StringProperty
import,该属性将保留当前gif图像源

from kivy.properties import StringProperty
-添加处理不同更改的方法:

class ExampleApp(App, BoxLayout):
    power = StringProperty('power_off.gif')

    def build(self):
        return self

    def change_state(self):
        if self.power == 'power_on.gif':
            self.power = 'power_off.gif'
        else:
            self.power = 'power_on.gif'
-然后将这些更改添加到您的kv中:

Builder.load_string("""
<ExampleApp>:
    id: main
    orientation: "vertical"
    Button:
        size_hint_x: None
        size_hint_y: None
        height: 300
        width: self.height
        center: self.parent.center
        text: ""
        on_press: gif.anim_delay = 0.09
        on_press: gif._coreimage.anim_reset(True); root.change_state()

        Image:
            id: gif
            source: root.power
            center: self.parent.center
            height: 300
            width: self.height
            allow_stretch: True
            anim_delay: -1
            anim_loop: 1


""")
Builder.load\u字符串(“”)
:
id:main
方向:“垂直”
按钮:
大小提示:无
尺寸提示:无
身高:300
宽度:自我高度
中心:self.parent.center
案文:“”
按下:gif.anim\u延迟=0.09
按:gif.\u coreimage.anim\u重置(True);root.change\u state()
图片:
id:gif
来源:root.power
中心:self.parent.center
身高:300
宽度:自我高度
允许拉伸:真
动画延迟:-1
动画循环:1
""")
Builder.load_string("""
<ExampleApp>:
    id: main
    orientation: "vertical"
    Button:
        size_hint_x: None
        size_hint_y: None
        height: 300
        width: self.height
        center: self.parent.center
        text: ""
        on_press: gif.anim_delay = 0.09
        on_press: gif._coreimage.anim_reset(True); root.change_state()

        Image:
            id: gif
            source: root.power
            center: self.parent.center
            height: 300
            width: self.height
            allow_stretch: True
            anim_delay: -1
            anim_loop: 1


""")