Python | Scatter in kivy

Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.

Scatter

Scatter is used to build interactive widgets that can be translated, rotated and scaled with two or more fingers on a multitouch system.

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



Just like in Relativelayout, the children are positioned relative to the scatter.
So when dragging the scatter, the position of the children don’t change, only the position of the scatter does.

The scatter size has no impact on the size of its children. If you want to resize it you can use scale, which transform both the scatter and its children but does not change size.

Basic Approach:
1) import kivy
2) import kivyApp
3) import scatter
4) import Relativelayout
5) import widget
6) Set minimum version(optional)
7) create Widget class
8) create layout class
9) create App class
10) create the, kv file
11) return Layout/widget/Class(according to requirement)
12) Run an instance of the class

To use scatter you must first have to import it by command:
from kivy.uix.scatter import Scatter

# Implementation of the Approach:

.py file

filter_none

edit
close

play_arrow

link
brightness_4
code

# Program to explain how to use Scatter in kivy  
       
# import kivy module     
import kivy   
         
# base Class of your App inherits from the App class.     
# app:always refers to the instance of your application    
from kivy.app import App  
       
# this restrict the kivy version i.e   
# below this kivy version you cannot   
# use the app or software   
kivy.require('1.9.0')  
  
# Scatter is used to build interactive
# widgets that can be translated,
# rotated and scaled with two or
# more fingers on a multitouch system.
from kivy.uix.scatter import Scatter
  
# Widgets are elements of a
# graphical user interface that
# form part of the User Experience.
from kivy.uix.widget import Widget
  
# This layout allows you to set relative coordinates for children. 
from kivy.uix.relativelayout import RelativeLayout
  
  
# Creating widget class
class SquareWidget(Widget):
    pass
  
# Creating Scatter Class
class ScatterWidget(Scatter):
    pass
  
# Create the layout class
class Scatter_App(RelativeLayout):
    pass
            
class ScatterApp(App):
    def build(self):
        return Scatter_App()
  
if __name__=='__main__':
    ScatterApp().run()

chevron_right


.kv file

filter_none

edit
close

play_arrow

link
brightness_4
code

# .kv file implementation
  
# Create the square to show scatter
<SquareWidget>:
    size: 100, 100
    canvas:
        Color:
            rgb: [0.345, 0.85, 0.456]
        Rectangle:
            size: self.size
            pos: self.pos
  
  
# Create the scatter properties           
<Scatter_App>:
      
    canvas:
        Color:
            rgb: .8, .5, .4
        Rectangle:
            size: self.size
            pos: self.pos
  
    ScatterWidget:
        id: square_widget_id
        SquareWidget:
  
    # Showing the current position of the box
    Label:
        text: 'Position: ' + str(square_widget_id.pos)
        size_hint: .1, .1
        pos: 500, 300

chevron_right


Output:



My Personal Notes arrow_drop_up

Competative Programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.