0

How to make an image always be in the center of an ellipse widget?

Below is the .py and .kv files along with the screen with the result of executing the code.

I have already tried in many ways and I can not do it. Could someone help me solve this problem?

fab.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
Window.clearcolor = (1, 1, 1, 1)

class Fab(App):
    pass

Fab().run()

fab.kv

Widget:
    canvas:
        Color:
            rgba: 0, 1, 0, 1
        Ellipse:
            pos: self.pos
            size: (dp(48), dp(48))
    Scatter:
        rotation: 45
        Image:
            source: 'plus.png'
            size: 64,64

enter image description here

rafaelcb21
  • 12,422
  • 28
  • 62
  • 86

1 Answers1

1

So, one reason why it is hard to do this simple thing is because you are not using specific widget to hold your shapes. I really recommend you do that because then you can use center_x and center_y attributes of the widget. So, make the widgets with the same size and position as you want your shapes to be and then just move the widgets themselves around. Observe:

Widget:
    Widget: # create a specific widget for the ellipse
        id: ellipse # give an id so we can refer to this widget
        size: (dp(48), dp(48))
        canvas:
            Color:
                rgba: 0, 1, 0, 1
            Ellipse:
            pos: self.pos
            size: self.size

    Scatter:
        size: (dp(48), dp(48))
        rotation: 45
        center_x: ellipse.center_x # refer to the ellipse
        center_y: ellipse.center_y
        Image:
            source: 'plus.png'

If you really need your image rotated, it's not the best idea to use a Scatter (you can drag the plus away from the ellipse, for example). Consider this thread.

Edvardas Dlugauskas
  • 1,469
  • 1
  • 12
  • 14