如何多次旋转小部件?

时间:2018-12-11 19:19:02

标签: python python-3.x rotation widget kivy

here中的官方示例显示了如何一次旋转小部件:

from kivy.app import App
from kivy.lang import Builder

kv = '''
FloatLayout:

    Button:
        text: 'hello world'
        size_hint: None, None
        pos_hint: {'center_x': .5, 'center_y': .5}
        canvas.before:
            PushMatrix
            Rotate:
                angle: 45
                origin: self.center
        canvas.after:
            PopMatrix
'''


class RotationApp(App):
    def build(self):
        return Builder.load_string(kv)


RotationApp().run()

但是如果我想创建一个旋转循环,即如何多次更改角度(并显示结果)?

1 个答案:

答案 0 :(得分:1)

将角度旋转量设置为可访问的NumericProperty附加位置(我将其附加至应用程序),然后在一个时钟间隔内对其进行更新

from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import NumericProperty

kv = '''
FloatLayout:

    Button:
        text: 'hello world'
        size_hint: None, None
        pos_hint: {'center_x': .5, 'center_y': .5}
        canvas.before:
            PushMatrix
            Rotate:
                angle: app.angle
                origin: self.center
        canvas.after:
            PopMatrix
'''


class RotationApp(App):
    angle = NumericProperty(25)
    def build(self):
        def updates_spin(*args):
            self.angle += 2

        Clock.schedule_interval(updates_spin, 0.05)
        return Builder.load_string(kv)


RotationApp().run()