Wednesday, July 2, 2014

42. Scatter

The Scatter class allows a user to drag, scale, and rotate any widget, which is under its control.




Besides the Scatter class, there has to be a Widget or any class based on Widget, like the Label or Image. The root class is based on RelativeLayout. We always have to include the App class.




The widget, scatter, and root class are created here. They are empty and will be defined in the kv file.




The app class must be defined and run. In the build, we return the root class.


# ex42.py

from kivy.uix.scatter import Scatter
from kivy.uix.widget import Widget
from kivy.uix.relativelayout import RelativeLayout
from kivy.app import App

class SquareWidget(Widget):
    pass

class ScatterWidget(Scatter):
    pass

class Ex42(RelativeLayout):
    pass
          
class Ex42App(App):
    def build(self):
        return Ex42()

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



In the kv file, we define the SquareWidget. It is a yellow square.




The background is a gray color.




The id of the ScatterWidget is given, so the 3 labels can refer to it's properties. The SquareWidget, is embedded, inside the Scatter. The first label, indicates the rotation, using the rotation value.




Labels 2 and 3 indicate, the scale, and the position, of the widget.


# ex42.kv

<SquareWidget>:
    size: 250,250
    canvas:
        Color:
            rgb: [1,1,0]
        Rectangle:
            size: self.size
            pos: self.pos
<Ex42>:
    canvas:
        Color:
            rgb: .5,.5,.5
        Rectangle:
            size: self.size
            pos: self.pos
    ScatterWidget:
        id: square_widget_id
        SquareWidget:
    Label:
        text: 'Rotation: ' + str(square_widget_id.rotation)
        size_hint: .1,.1
        pos: 500,100
    Label:
        text: 'Scale: ' + str(square_widget_id.scale)
        size_hint: .1,.1
        pos: 500,200
    Label:
        text: 'Position: ' + str(square_widget_id.pos)
        size_hint: .1,.1
        pos: 500,300


Demo of App on Youtube

2 comments:

  1. Python:

    Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    https://www.emexotechnologies.com/online-courses/python-training-in-electronic-city/

    ReplyDelete