关于磁铁: https://github.com/kivy-garden/garden.magnet
这是一个工作磁铁小部件(我改变了一点)
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ListProperty, DictProperty
from kivy.animation import Animation
class Magnet(Widget):
transitions = DictProperty({'pos': 'in_out_quad', 'size': 'in_out_quad'})
duration = NumericProperty(1)
anims = ListProperty([])
def __init__(self, **kwargs):
super(Magnet, self).__init__(**kwargs)
self.bind(**{k: self.attract for k in self.transitions})
def on_children(self, *args):
if len(self.children) > 1:
raise ValueError('Magnet can have only one children')
else:
self.attract()
def attract(self, *args):
if not self.children:
return
if self.anims:
for a in self.anims:
a.stop(self.children[0])
self.anims = []
for t in self.transitions:
a = Animation(t=self.transitions[t], d=self.duration,
**{t: getattr(self, t), })
a.start(self.children[0])
self.anims.append(a)
我想把它改成不是作为父母,而是作为孩子。你知道吗
我是说: 默认磁铁:
Magnet
SomeWidget
我想实现的目标:
SomeWidget
Magnet
我尝试将“children”改为“parent”,但这个示例不起作用:
class Magnet(Widget):
transitions = DictProperty({'pos': 'in_out_quad', 'size': 'in_out_quad'})
duration = NumericProperty(1)
anims = ListProperty([])
def __init__(self, **kwargs):
super(Magnet, self).__init__(**kwargs)
self.bind(**{k: self.attract for k in self.transitions})
def on_parent(self, *args):
self.attract()
def attract(self, *args):
if not self.parent:
return
if self.anims:
for a in self.anims:
a.stop(self.parent)
self.anims = []
for t in self.transitions:
a = Animation(t=self.transitions[t], d=self.duration,
**{t: getattr(self, t), })
a.start(self.parent)
self.anims.append(a)
我错过了什么?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐