小时候的磁铁小部件

时间:2019-05-17 23:16:18

标签: python kivy

关于磁铁: 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)

我想念什么?

0 个答案:

没有答案