Python / Kivy / Gif:如何在动画后删除 gif

发布于 2025-01-14 17:41:25 字数 1722 浏览 7 评论 0原文

我想要一个 Gif 作为开场动画,在动画结束后将其删除或移走。然而似乎没有任何作用,图像只是停留在那里。

from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.textinput import TextInput
from kivy.core.text import LabelBase
from kivy.uix.image import Image
from kivy.properties import StringProperty
from kivy.lang import Builder
from kivy.factory import Factory
from kivymd.app import App
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.extras.highlight import KivyLexer
from kivy.lang.parser import ParserException
from kivy.properties import ColorProperty
from kivymd.uix.label import MDLabel
from kivy.animation import Animation
from kivy.uix.screenmanager import ScreenManager, Screen

import time
import threading
import random

kv = ("""   
FloatLayout:
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'test_back.png'

    Image:
        id: intro
        source: 'test_anim.gif'
        size_hint: None, None
        size: "500dp", "500dp"
        pos_hint: {'center_x':.5, 'center_y':.5}
        allow_stretch: False
        anim_delay: 0.05
        anim_loop: 1

""")

class testApp(App):
    def build(self):
        kivy_design = Builder.load_string(kv)
        self.app_root = kivy_design
        return kivy_design

    def on_start(self):
        frame_counter = 0
        frame_counter += 1
        if frame_counter == 30:
            self.app_root.ids.intro.pos_hint = {'center_x':-.5, 'center_y':-.5}

testApp().run()

我现在尝试了相当长一段时间,也研究了其他类似的问题。

我缺少什么?感谢您的阅读

I want to have a Gif as an opening animation that is deleted or moved away after the animation. However nothing seems to work, the image just stays there.

from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.textinput import TextInput
from kivy.core.text import LabelBase
from kivy.uix.image import Image
from kivy.properties import StringProperty
from kivy.lang import Builder
from kivy.factory import Factory
from kivymd.app import App
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.extras.highlight import KivyLexer
from kivy.lang.parser import ParserException
from kivy.properties import ColorProperty
from kivymd.uix.label import MDLabel
from kivy.animation import Animation
from kivy.uix.screenmanager import ScreenManager, Screen

import time
import threading
import random

kv = ("""   
FloatLayout:
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'test_back.png'

    Image:
        id: intro
        source: 'test_anim.gif'
        size_hint: None, None
        size: "500dp", "500dp"
        pos_hint: {'center_x':.5, 'center_y':.5}
        allow_stretch: False
        anim_delay: 0.05
        anim_loop: 1

""")

class testApp(App):
    def build(self):
        kivy_design = Builder.load_string(kv)
        self.app_root = kivy_design
        return kivy_design

    def on_start(self):
        frame_counter = 0
        frame_counter += 1
        if frame_counter == 30:
            self.app_root.ids.intro.pos_hint = {'center_x':-.5, 'center_y':-.5}

testApp().run()

I tried now for quite some time and also looked at other similar issues like this.

What am I missing? Thanks for reading

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吐个泡泡 2025-01-21 17:41:25

您可以安排删除intro,并继续延迟删除,直到gif 停止。这是执行此操作的代码的修改版本:

from kivy.clock import Clock
from kivy.lang import Builder
from kivymd.app import App

kv = ("""   
FloatLayout:
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'test_back.png'

    Image:
        id: intro
        source: 'test_anim.gif'
        size_hint: None, None
        size: "500dp", "500dp"
        pos_hint: {'center_x':.5, 'center_y':.5}
        allow_stretch: False
        anim_delay: 0.05
        anim_loop: 1
        on_texture: app.texture_updated()  # this lets us know every frame

""")

class testApp(App):
    def build(self):
        kivy_design = Builder.load_string(kv)
        self.app_root = kivy_design
        return kivy_design

    def on_start(self):
        # schedule removal of intro gif
        self.remove_it = Clock.schedule_once(self.remove_gif, self.root.ids.intro.anim_delay * 2)

    def texture_updated(self):
        # gif has been updated, so cancel removal
        self.remove_it.cancel()

        # reschedule removal
        self.remove_it = Clock.schedule_once(self.remove_gif, self.root.ids.intro.anim_delay * 2)

    def remove_gif(self, dt):
        # actually remove the intro gif
        self.root.remove_widget(self.root.ids.intro)



testApp().run()

You can schedule the removal of the intro, and keep delaying that removal until the gif stops. Here is a modified version of your code that does that:

from kivy.clock import Clock
from kivy.lang import Builder
from kivymd.app import App

kv = ("""   
FloatLayout:
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'test_back.png'

    Image:
        id: intro
        source: 'test_anim.gif'
        size_hint: None, None
        size: "500dp", "500dp"
        pos_hint: {'center_x':.5, 'center_y':.5}
        allow_stretch: False
        anim_delay: 0.05
        anim_loop: 1
        on_texture: app.texture_updated()  # this lets us know every frame

""")

class testApp(App):
    def build(self):
        kivy_design = Builder.load_string(kv)
        self.app_root = kivy_design
        return kivy_design

    def on_start(self):
        # schedule removal of intro gif
        self.remove_it = Clock.schedule_once(self.remove_gif, self.root.ids.intro.anim_delay * 2)

    def texture_updated(self):
        # gif has been updated, so cancel removal
        self.remove_it.cancel()

        # reschedule removal
        self.remove_it = Clock.schedule_once(self.remove_gif, self.root.ids.intro.anim_delay * 2)

    def remove_gif(self, dt):
        # actually remove the intro gif
        self.root.remove_widget(self.root.ids.intro)



testApp().run()
南街九尾狐 2025-01-21 17:41:25

我不确定编码的答案是什么,但理论上可能是你找到了 GIF 的长度。在一个页面上播放 GIF,并设置 GIF 长度的显示时间,然后再显示另一个页面,显示您想要的内容。

I am not sure what the coded answer will be, but the theory could be that you find the length of the GIF. Play the GIF on a page and set the time for display for the length of the GIF then just display another page with what you want afterwards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文