Home > Software engineering >  How to rotate a widget forward another?
How to rotate a widget forward another?

Time:10-27

I would like to know how I can rotate a widget forward another. Here is what I have: (I use scatter and animation to rotate my widget, and I don't want to use .kv file here)

terrain = Image(source = "terrain.png")

target = Image(source = "target.png", center = (300, 200))
terrain.add_widget(target)

arrow = Image(source = "arrow.png", center = (100, 150))
scatter = Scatter(rotation = 90) 
scatter.add_widget(arrow)
terrain.add_widget(scatter)

animation = Animation(center = (target.center_x, target.center_y), rotation = ____)#<forward target
animation.start(scatter)

Hope you can answer, Tarezze.

CodePudding user response:

I think it is simpler to just use context instructions, rather than a Scatter widget. Here is an example (based on your posted code):

class TestApp(App):
    def build(self):

        root = FloatLayout()  # use a Layout widget to contain the Images

        terrain = Image(source = "terrain.png")
        root.add_widget(terrain)

        target = Image(source = "target.png", center = (300, 200))
        root.add_widget(target)

        self.arrow = Image(source = "arrow.png", center = (100, 150))

        # set up Rotation using Context Instructions in the Canvas
        with self.arrow.canvas.before:
            PushMatrix()
            self.arrow.rotate = Rotate(angle=0, origin=self.arrow.center)
        with self.arrow.canvas.after:
            PopMatrix()

        root.add_widget(self.arrow)

        # schedule the rotation
        Clock.schedule_once(self.do_rotate, 2)

        return root

    def do_rotate(self, dt):
        self.arrow.rotate.origin = self.arrow.center
        animation = Animation(angle=-90)
        animation.start(self.arrow.rotate)
  • Related