androidpythonformattingkivyscreens

Kivy (Python) Problems with formatting


I'm having trouble formatting these Ellipses so that they appear in the center of the screen. I've tried everything I can think of to an avail. The Button I've written in the Kivy language goes to the corner of the monitor like it's meant to but everything in the Target class refuses to obey my formatting and is drawn on the bottom left of the screen no matter what.

class Target(Label):
    def __init__(self, **kwargs):
        super(Target, self).__init__(**kwargs)
        with self.canvas:
            Color(1,1,1)
            d = 400
            Ellipse(pos=((self.center_x - d / 2), (self.center_y - d / 2)), size=(d,d))

            Color(0,0,0)
            d = 320
            Ellipse(pos=((self.center_x - d / 2), (self.center_y - d / 2)), size=(d,d))

            Color(0,0,1)
            d = 240
            Ellipse(pos=((self.center_x - d / 2), (self.center_y - d / 2)), size=(d,d))

            Color(1,0,0)
            d = 160
            Ellipse(pos=((self.center_x - d / 2), (self.center_y - d / 2)), size=(d,d))

            Color(1,1,0)
            d = 80
            Ellipse(pos=((self.center_x - d / 2), (self.center_y - d / 2)), size=(d,d))

That's all the raw python code pertaining to drawing the circles on the widget. Here's the Kivy code that deals with the screen.

<ScoringLayout>:
    FloatLayout:
        Target:
            center: self.parent.center
            size_hint: 0.2, 0.3

        Button:
            text: "Return"
            on_release: app.root.current = "main"
            font_size: 15
            size_hint: 0.3 ,0.2
            pos_hint: {"right": 1, "bottom": 1}

Scoring Layout is the layout for a screen that is switched to, the button underneath the broken code links back to the main screen of the app.

I should also say that the goal is for this to be packaged as an Android app along with the rest of my code.


Solution

  • The Ellipses are drawn according to self.center_x at the moment when __init__ is run, at which point it is still the default of (50, 50).

    You can fix it by using kv language, or manually creating a binding to a function that updates the ellipse pos and size when the widget pos or size change.