[英]Kivy non-GUI events
我正在嘗試在首次加載應用程序時啟動動畫。 立即關閉加載屏幕后的IE。 我已經厭倦了“ 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>:
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())
on_enter
方法是在Screen
定義的,而不是在Widget
。 您應該將該矩形放置在屏幕上(“ Root
窗口小部件應該是此處的屏幕),並且一旦屏幕的on_enter
事件觸發,就啟動矩形動畫。
同樣,您調用它的方式不正確; 函數調用應包含方括號,即on_enter: self.start_animation()
看起來像您想要的東西嗎?
我剛剛刪除了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())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.