Scatter size and position issues

I am facing a problem with a scattering object. From my codes below. After resizing Scatter

( self.size_hint_x

, self.size_hint_y = 0.3, 0.3

), objects ( canvas

, label

) inside Scatter

do not change either. I tried to apply size_hint=1

to canvas

and label

inside Scatter

, but the result is still the same.

Another problem I ran into was getting the position X, Y

(relative to the parent) for canvas

/ label

in Scatter

. He always gives me (0,0)

.

My code

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.scatter import Scatter
from kivy.graphics import Color, Rectangle, Canvas

class Avatar(Scatter):
    def __init__(self, **kwargs):
        super(Avatar, self).__init__(size_hint=(None,None), **kwargs)

        with self.canvas:
            Color(0, 0, 0)
            Rectangle(pos=(self.x, self.y), size=(self.width, self.height))

        self.lbl = Label(text='Test', size_hint_x=1, size_hint_y=1)
        self.add_widget(self.lbl)

        # Scatter size is 30% of the GameBackground
        # ISSUE: After resize my Scatter, the objects inside is not resized as well.
        self.size_hint_x, self.size_hint_y = 0.3, 0.3


class GameBackground(FloatLayout):
    def __init__(self, **kwargs):
        super(GameBackground, self).__init__(**kwargs)

        with self.canvas:
            Color(1, 0, 1)
            Rectangle(pos = (0, 0), size = (Window.width,Window.height))

        self.elf = Avatar()
        self.add_widget(self.elf)
        self.elf.x = 200
        self.elf.y = 300

        # Get the X, Y position of the Scatter and the label inside the Scatter relative to the parent.
        print self.elf.pos      #<-- This works.
        print self.elf.lbl.pos  #<-- ISSUE: This not working.


class GameApp(App):
    def build(self):
        return GameBackground()


if __name__ == '__main__':
    GameApp().run()

      

Did I miss something? Thanks for any advice.

I am new to Kivy. So forgive me if my qns are dumb.: P

+2


source to share


1 answer


You have read the documentation for Scatter . It says that

... the specific behavior makes the scattering unique, and there are some advantages / limitations you should consider:

  • Children are positioned relative to 0, 0. The position of the scatter does not affect the position of the children.
  • This also applies to size. If you want to resize the scatter, use scale, not size. (read # 1.)

This is the answer to your first question. It indicates the scale of use, not the size . There is also an apply_transform method that you might find useful for other transformations. I have never tried this method, but I cannot see the translation value (I can see Rotate and Scale)

Regarding your second question. You add a rectangle at position self.x

and self.y

, which is (0,0). So, your Rectangle is at this position. If you drag (using your fingers or mouse) your widget. The position of the Rectangle remains relative Scatter

. So, unless you change the position of the rectangle (with code), it will always be at (0,0). Transformations always refer to Scatter.

This question can be related and explains several issues without using the Kivy language to add Vertex Instructions (i.e. Rectangles). You have to consider this, because what you are doing seems to be related.

* EDIT - only mandatory fixes according to my understanding of what you are trying to achieve *

1) Don't use sizing cues like the ones you are using.

1.1) Instead of:

self.lbl = Label(text='Test', size_hint_x=1, size_hint_y=1)

      

using:

self.lbl = Label(text='Test', width=self.width, height=self.height)

      



1.2) And instead of:

self.size_hint_x, self.size_hint_y = 0.3, 0.3

      

using:

self.scale = 0.3

      

2) Position refers to the spread . Therefore, you need to transfer the coordinates to the parent.

2.1) Instead of:

print self.elf.lbl.pos  #<-- ISSUE: This not working.

      

using:

print self.elf.to_parent(*self.elf.lbl.pos)

      

Here's the complete code:

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.scatter import Scatter
from kivy.graphics import Color, Rectangle, Canvas

class Avatar(Scatter):
    def __init__(self, **kwargs):
        super(Avatar, self).__init__(size_hint=(None,None), **kwargs)

        with self.canvas:
            Color(0, 0, 0)
            Rectangle(pos=(self.x, self.y), size=(self.width, self.height))

        #self.lbl = Label(text='Test', size_hint_x=1, size_hint_y=1)
        self.lbl = Label(text='Test', width=self.width, height=self.height)
        self.add_widget(self.lbl)

        # Scatter size is 30% of the GameBackground
        # ISSUE: After resize my Scatter, the objects inside is not resized as well.
        # self.size_hint_x, self.size_hint_y = 0.3, 0.3
        self.scale = 0.3


class GameBackground(FloatLayout):
    def __init__(self, **kwargs):
        super(GameBackground, self).__init__(**kwargs)

        with self.canvas:
            Color(0, 0, 1)
            Rectangle(pos = (0, 0), size = (Window.width,Window.height))

        self.elf = Avatar()
        self.add_widget(self.elf)
        self.elf.x = 200
        self.elf.y = 300

        # Get the X, Y position of the Scatter and the label inside the Scatter relative to the parent.
        print self.elf.pos      #<-- This works.
        print self.elf.lbl.pos  #<-- ISSUE: This not working.
        print self.elf.to_parent(*self.elf.lbl.pos)

class GameApp(App):
    def build(self):
        return GameBackground()

if __name__ == '__main__':
    GameApp().run()

      

+4


source







All Articles