Table of contents
1.
Introduction
2.
About kivy
3.
Scatter 
3.1.
Implementation
3.1.1.
Python File
3.1.2.
scatter.kv File
3.2.
Explanation
3.3.
Output
4.
FAQs
4.1.
What is Kivy?
4.2.
What is the Scatter widget in Kivy?
4.3.
What is the widget in Kivy?
4.4.
What is the root of Kivy?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Scatter in Kivy

Author Aniket Majhi
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome readers! We hope you are doing well.

This blog is going to be interesting for you. Nowadays, Python has become one of the most important tools in developing different applications from a programmer's perspective. It has a lot of library support. If you want to impact the development field in Python, you need to learn more about different libraries in Python.

If you want to learn more about python, you can refer to the link Basics of Python.

In this blog, we will be discussing the Scatter in the Kivy library in Python. We will cover every aspect of the Scatter Widget.

This blog will help enhance your knowledge about the Kivy library in Python and help you stand out from the crowd.

About kivy

Kivy is an open-source platform-independent GUI development python library for developing innovativemulti-touch UI applications. It can run on WindowsGNU/LinuxAndroidiOS etc.

Kivy is mainly used to develop Android Applications. It is also used in developing the Desktop Applications as well. 

Scatter 

The Scatter in kivy is used for building interactive widgets that can be rotated, scaled and translated with two or more fingers on a multi-touch system.

A few points to be considered in the Scatter are shown below:

  1. The scatter has its matrix transformation. It allows the user to translate, drag and rotate any widget.
     
  2. The scatter children are positioned relative to the scatter, similar to RelativeLayout. While dragging the scatter, the position of the children doesn’t change. Only the position of the scatter changes.
     
  3. The scatter size has no impact on the size of its children. To resize the scatter, you should use the scale that transforms the scatter and its children but does not change the size.
     

Below, we will be showing the implementation of the Scatter widget.

Implementation

The implementation is as follows:

Python File

# import kivy module    
import kivy
from kivy.app import App
from kivy.lang import Builder

# If you want to work with the Scatter, you must include this library
from kivy.uix.scatter import Scatter

# Widgets are the elements of the GUI
from kivy.uix.widget import Widget

# It allows you to set relative coordinates for children.
from kivy.uix.relativelayout import RelativeLayout

# load the scatter.kv file
Builder.load_file('scatter.kv')

# widget class
class SquareWidget(Widget):
    pass

# Scatter Class
class ScatterWidget(Scatter):
    pass

# layout class
class ScatterLayout(RelativeLayout):
    pass

# ScatterApp class
class ScatterApp(App):
    def build(self):
        return ScatterLayout()

ScatterApp().run()
You can also try this code with Online Python Compiler
Run Code

scatter.kv File

# Square to show scatter
<SquareWidget>:
size: 100, 100
canvas:
Color:
rgb: [34 / 255, 139 / 255, 34 /255]
Rectangle:
size: self.size
pos: self.pos



# Scatter properties 
<ScatterLayout>:
canvas:
Color:
rgb: [0 , 0 , 1]
Rectangle:
size: self.size
pos: self.pos


ScatterWidget:
id: square_widget_id
SquareWidget:

Explanation

  • First, we imported the required headers.
  • Then we created three custom widgets, namely SquareWidgetScatterWidget and ScatterLayout, leaving blank using pass.
  • We inherited the subclass, namely ScatterApp, from the superclass App.
  • Inside the subclass ScatterApp, we overrode the build function that returns the ScatterLayout Widget.
  • The  ScatterApp.run()  builds the app, which returns the ScatterLayout widget.
  • We created the scatter.kv file, which contains the layout of the widgets.
  • We loaded the scatter.kv file using Builder.load_file(“scatter.kv”)
  • The custom widgets are accessed using the angular braces(“<>”) in scatter.kv file.
  • Each of the widgets contains attributes which specify the layout.
  • The ScatterLayout widget contains the ScatterWidget widget as an attribute, and the ScatterWidget widget contains the SquareWidget widget as an attribute.
  • The ScatterLayout forms the blue coloured outer layout, inside which the ScatterWidget is defined that contains the SquareWidget, which forms the green coloured square part inside it.

Output

FAQs

What is Kivy?

Kivy is an open-source platform-independent GUI development python library for developing applications.

What is the Scatter widget in Kivy?

The Scatter in kivy is used for building interactive widgets that can be rotated, scaled and translated with two or more fingers on a multi-touch system.

What is the widget in Kivy?

The widget in Kivy is the base building block of the GUI interface.

What is the root of Kivy?

The root inside a kv file refers to the parent with the angular brackets.

Conclusion

In this article, we have extensively discussed the Scatter in Kivy.

  • We started with the basic introduction. 
  • We discussed the Kivy
  • We looked at what a Scatter is in Kivy. 
  • We showed the implementation of the Scatter in Kivy.
     

We hope that this blog has helped you enhance your knowledge regarding Scatter in Kivy. If you would like to learn more, check out our articles on Open Source Python Libraries and Application Development in PythonKivy Float Layout in Python, and Popular Python Libraries. Do upvote our blog to help other ninjas grow.

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, follow our guided paths, and crack product based companies Interview Bundle.

Happy Reading!

Live masterclass