Olive Yew
Olive Yew

Reputation: 371

Kivy create pulsing background

I'm trying to create pulsing background in my app. Following this question Pulsing Background Color in Kivy

i've tried to make it work also, but for some reason it doesn't work. Here's my main.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.core.window import Window
from kivymd.app import MDApp
from kivy.uix.image import Image
from kivy.animation import Animation
from kivy.clock import Clock

Window.size = (360,600)

class MyGridLayout(Screen):    
   def blink(self):
       x = Clock.schedule_once(self.start_pulsing,2)
       return x

   def start_pulsing(self,*args):
       anim = Animation(animated_color=(1,0,0,1))
       anim.start(self)
       anim.repeat = True


class mainApp(App):
  def build(self):
    return MyGridLayout()

if __name__ == '__main__':
  mainApp().run()

And my main.kv file

<MyGridLayout>:
    FloatLayout:
        size: root.width, root.height
        animated_color:(1,1,1,1)
        canvas.before:
            Color:
                rgba :self.animated_color
            Rectangle:
                pos: self.pos
                size: self.size

Any help will be much appreciated

Upvotes: 0

Views: 415

Answers (1)

John Anderson
John Anderson

Reputation: 38962

A couple problems with your code:

  • The animated_color property in your kv is a property of the FloatLayout, but your python code references it as a property of MyGridLayout.
  • The anim.repeat = True will cause the Animation to be repeated, but after the first animation, the animated_color is already (1,0,0,1), so the repeated Animation will have no effect.
  • I don't see any code calling the blink() method to actually start the pulsing.

Here is a modified version of your code that reflects the above changes:

from kivy.app import App
from kivy.properties import ColorProperty
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.core.window import Window
from kivy.animation import Animation
from kivy.clock import Clock

Window.size = (360,600)

class MyGridLayout(Screen):
   animated_color = ColorProperty()  # the color property to be animated
   pulse_interval = 4  # the interval for pulsing

   def blink(self):
       x = Clock.schedule_once(self.start_pulsing,2)
       return x

   def start_pulsing(self,*args):
       d = self.pulse_interval /2
       anim = Animation(animated_color=(1,0,0,1), duration=d) + \
              Animation(animated_color=(1,1,1,1), duration=d)
       anim.repeat = True
       anim.start(self)

class mainApp(App):
  def build(self):
    root = MyGridLayout()
    root.blink()  # star the pulsing
    return root

if __name__ == '__main__':
  mainApp().run()

And the modified main.kv:

<MyGridLayout>:
    FloatLayout:
        size: root.width, root.height
        # animated_color:(1,1,1,1)  # property needs to be attribute of MyGridLayout
        canvas.before:
            Color:
                rgba :root.animated_color  # reference the animated_color property of MyGridLayout
            Rectangle:
                pos: self.pos
                size: self.size

Upvotes: 1

Related Questions