Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kivy Label text fade in

Tags:

python

kivy

I'm trying to make some text in a label fade in using the animation tool in kivy but I can't get it to work and I found NOTHING on the internet to help anywhere. Heres the code:

.py:

class TestScreen(Screen):
    def animate (self):
        anim = Animate(opacity=1, duration=2)
        anim.start(self.lbl)

.kv

<TestScreen>
    lbl: label
    Label
        id: label
        text: "Welcome"

1 Answers

  • change Animate with Animation
  • opacity=1 means label is visible, what you want is opacity=0
  • you should call animate function somewhere

Here's fully working example (Python 2.7):

from __future__ import absolute_import, division, print_function, unicode_literals
__metaclass__ = type

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.animation import Animation


Builder.load_string(b'''
<RootWidget>:
    lbl: label
    Label
        id: label
        text: "Welcome"
''')


class RootWidget(Screen):
    def __init__(self, **kwargs):
        super(RootWidget, self).__init__(**kwargs)
        self.animate()

    def animate(self):
        anim = Animation(opacity=0, duration=2)
        anim.start(self.lbl)


class TestApp(App):
    def build(self):
        return RootWidget()


if __name__ == '__main__':
    TestApp().run()
like image 166
Mikhail Gerasimov Avatar answered Dec 06 '25 19:12

Mikhail Gerasimov