2016-09-12 5 views
0

アプリが最初に読み込まれたときにアニメーションを開始しようとしています。 I.ロード画面が閉じられた直後。私は "on_enter"イベントに疲れましたが、うまくいかないようですが、どんな助けでも大歓迎です。Kivy非GUIイベント

from kivy.base import runTouchApp 
from kivy.lang import Builder 
from kivy.uix.widget import Widget 
from kivy.animation import Animation 
from kivy.properties import ListProperty 
from kivy.core.window import Window 
from random import random 
from kivy.graphics import Color, Rectangle 

Builder.load_string(''' 
<Root>: 
AnimRect: 
    pos: 500, 300 
<AnimRect>: 
on_enter: self.start_animation 
canvas: 
    Color: 
     rgba: 0, 1, 0, 1 
    Rectangle: 
     pos: self.pos 
     size: self.size 
''') 

class Root(Widget): 
pass 

class AnimRect(Widget): 
    def anim_to_random_pos(self): 
     Animation.cancel_all(self) 
     random_x = random() * (Window.width - self.width) 
     random_y = random() * (Window.height - self.height) 

     anim = Animation(x=random_x, y=random_y, 
        duration=4, 
        t='out_elastic') 
     anim.start(self) 

    def on_touch_down(self, touch): 
     if self.collide_point(*touch.pos): 
      self.anim_to_random_pos() 

    def start_animation(self, touch): 
     if self.collide_point(*touch.pos): 
      self.anim_to_random_pos() 

runTouchApp(Root()) 

print screen of error

答えて

0

on_enter方法はScreenではなく、Widgetで定義されています。その矩形を画面に配置する必要があります(ここではRootウィジェットを画面にする必要があります)。画面のon_enterイベントが発生すると、矩形アニメーションを開始します。

また、間違って呼び出しています。関数呼び出しに角括弧が含まれている必要があります。つまり、on_enter: self.start_animation()

0

あなたが望むようなものですか?

あなたのkvの "on_enter"行を削除して、インデントを修正しました。

from kivy.base import runTouchApp 
from kivy.lang import Builder 
from kivy.uix.widget import Widget 
from kivy.animation import Animation 
from kivy.properties import ListProperty 
from kivy.core.window import Window 
from random import random 
from kivy.graphics import Color, Rectangle 

Builder.load_string(''' 
<Root>: 
    AnimRect: 
     pos: 500, 300 
<AnimRect>: 
    canvas: 
     Color: 
      rgba: 0, 1, 0, 1 
     Rectangle: 
      pos: self.pos 
      size: self.size 
''') 

class Root(Widget): 
    pass 

class AnimRect(Widget): 
    def anim_to_random_pos(self): 
     Animation.cancel_all(self) 
     random_x = random() * (Window.width - self.width) 
     random_y = random() * (Window.height - self.height) 

     anim = Animation(x=random_x, y=random_y, 
        duration=4, 
        t='out_elastic') 
     anim.start(self) 

    def on_touch_down(self, touch): 
     if self.collide_point(*touch.pos): 
      self.anim_to_random_pos() 

    def start_animation(self, touch): 
     if self.collide_point(*touch.pos): 
      self.anim_to_random_pos() 

runTouchApp(Root()) 
関連する問題