在 Kivy 中同步动画
Animations in the same time in Kivy
我正在编写一个简单的视错觉应用程序,以学习如何在 Kivy 中编写代码。我想知道我的程序为什么会崩溃以及如何解决。
如果我取消注释 animation.start(self.c4) 行,那么我的程序运行良好,所以我几乎可以肯定问题是两个动画同时是 运行。错误是什么以及如何解决?
我的 .py 文件:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ObjectProperty,\
ReferenceListProperty
from kivy.graphics import Color
from kivy.core.window import Window
from kivy.animation import Animation
class Circle(Widget):
v = NumericProperty(0)
class Illusion(Widget):
c1 = ObjectProperty(None)
c2 = ObjectProperty(None)
c3 = ObjectProperty(None)
c4 = ObjectProperty(None)
def __init__(self, **kwargs):
super(Illusion, self).__init__(**kwargs)
objects = (self.c1, self.c2, self.c3, self.c4)
values = (0,1,0,0)
for i, item in enumerate(objects):
objects[i].v = values[i]
self.start_illusion(instance = None, value = None)
def start_illusion(self, instance, value):
animation = Animation(v = 1, d = .25)
animation.bind(on_complete=self.continue_illusion)
animation.start(self.c3)
animation.start(self.c4)
def continue_illusion(self, instance, value):
animation = Animation(v = 0, d = .25)
animation.bind(on_complete=self.start_illusion)
animation.start(self.c3)
animation.start(self.c4)
class IllusionsApp(App):
Window.clearcolor = (0,0,1,1)
def build(self):
return Illusion()
if __name__ == '__main__':
IllusionsApp().run()
我的 .kv 文件:
<Circle>:
canvas:
Color:
hsv: 0,0,self.v
Ellipse:
pos: self.pos
size: self.size
<Illusion>:
c1: _c1
c2: _c2
c3: _c3
c4: _c4
Circle:
id: _c1
size: 250,250
center_x: self.parent.width / 4
center_y: self.parent.height / 2
Circle:
id: _c2
size: 250,250
center_x: self.parent.width * 3/4
center_y: self.parent.height / 2
Circle:
id: _c3
size: 180,180
center_x: self.parent.width / 4
center_y: self.parent.height / 2
Circle:
size: 180,180
id: _c4
center_x: self.parent.width * 3/4
center_y: self.parent.height / 2
我认为问题可能出在每次调用 on_complete
函数时重新创建新的 Animation
。
尝试使用repeat animation property.
def __init__(self, **kwargs):
super(Illusion, self).__init__(**kwargs)
objects = (self.c1, self.c2, self.c3, self.c4)
values = (0,1,0,0)
for i, item in enumerate(objects):
objects[i].v = values[i]
self.animation_v0 = Animation(v = 0, d = .25) + Animation(v = 1, d = .25)
self.animation_v0.repeat = True
self.animation_v1 = Animation(v = 1, d = .25) + Animation(v = 0, d = .25)
self.animation_v1.repeat = True
#self.animation_v0.bind(on_complete=self.start_illusion)
#self.animation_v1.bind(on_complete=self.continue_illusion)
self.animation_v0.start(self.c3)
self.animation_v1.start(self.c4)
def start_illusion(self, instance, value):
self.animation_v0.start(self.c3)
def continue_illusion(self, instance, value):
self.animation_v1.start(self.c4)
我正在编写一个简单的视错觉应用程序,以学习如何在 Kivy 中编写代码。我想知道我的程序为什么会崩溃以及如何解决。 如果我取消注释 animation.start(self.c4) 行,那么我的程序运行良好,所以我几乎可以肯定问题是两个动画同时是 运行。错误是什么以及如何解决?
我的 .py 文件:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ObjectProperty,\
ReferenceListProperty
from kivy.graphics import Color
from kivy.core.window import Window
from kivy.animation import Animation
class Circle(Widget):
v = NumericProperty(0)
class Illusion(Widget):
c1 = ObjectProperty(None)
c2 = ObjectProperty(None)
c3 = ObjectProperty(None)
c4 = ObjectProperty(None)
def __init__(self, **kwargs):
super(Illusion, self).__init__(**kwargs)
objects = (self.c1, self.c2, self.c3, self.c4)
values = (0,1,0,0)
for i, item in enumerate(objects):
objects[i].v = values[i]
self.start_illusion(instance = None, value = None)
def start_illusion(self, instance, value):
animation = Animation(v = 1, d = .25)
animation.bind(on_complete=self.continue_illusion)
animation.start(self.c3)
animation.start(self.c4)
def continue_illusion(self, instance, value):
animation = Animation(v = 0, d = .25)
animation.bind(on_complete=self.start_illusion)
animation.start(self.c3)
animation.start(self.c4)
class IllusionsApp(App):
Window.clearcolor = (0,0,1,1)
def build(self):
return Illusion()
if __name__ == '__main__':
IllusionsApp().run()
我的 .kv 文件:
<Circle>:
canvas:
Color:
hsv: 0,0,self.v
Ellipse:
pos: self.pos
size: self.size
<Illusion>:
c1: _c1
c2: _c2
c3: _c3
c4: _c4
Circle:
id: _c1
size: 250,250
center_x: self.parent.width / 4
center_y: self.parent.height / 2
Circle:
id: _c2
size: 250,250
center_x: self.parent.width * 3/4
center_y: self.parent.height / 2
Circle:
id: _c3
size: 180,180
center_x: self.parent.width / 4
center_y: self.parent.height / 2
Circle:
size: 180,180
id: _c4
center_x: self.parent.width * 3/4
center_y: self.parent.height / 2
我认为问题可能出在每次调用 on_complete
函数时重新创建新的 Animation
。
尝试使用repeat animation property.
def __init__(self, **kwargs):
super(Illusion, self).__init__(**kwargs)
objects = (self.c1, self.c2, self.c3, self.c4)
values = (0,1,0,0)
for i, item in enumerate(objects):
objects[i].v = values[i]
self.animation_v0 = Animation(v = 0, d = .25) + Animation(v = 1, d = .25)
self.animation_v0.repeat = True
self.animation_v1 = Animation(v = 1, d = .25) + Animation(v = 0, d = .25)
self.animation_v1.repeat = True
#self.animation_v0.bind(on_complete=self.start_illusion)
#self.animation_v1.bind(on_complete=self.continue_illusion)
self.animation_v0.start(self.c3)
self.animation_v1.start(self.c4)
def start_illusion(self, instance, value):
self.animation_v0.start(self.c3)
def continue_illusion(self, instance, value):
self.animation_v1.start(self.c4)