Change language

Python | Canvas in Kiwi using .kv file

| |

Kivy — it is a platform independent GUI tool in Python. Since it can run on Android, IOS, Linux, Windows, etc. It is mainly used to develop Android application, but that does not mean that it cannot be used in desktop applications.

Canvas:

Canvas — this is the root object used for drawing by the widget. Kiwi canvas is not where you paint. 
Every widget in Kivy already has a default Canvas. When you create a widget, you can create all the instructions you need to draw. If you — this is your current widget. The Color and Rectangle statements are automatically added to the canvas and will be used when drawing the window.

To use Canvas, you must import:

 from kivy.graphics import Rectangle, Color 
  Basic Approach -  -" import kivy -" import kivy App -" import widget -" import Canvas ie: from kivy.graphics import Rectangle, Color -" set minimum version (optional) -" Extend the Widget class -" Create the App Class -" create the .kv file: -" create the canvas -" Add action / callback if needeed -" return a Widget -" Run an instance of the class 

Implementation of the approach:

1) Creating a simple canvas:

main.py file

# import nodded module

import kivy 

  
# this limits the kivy version ie
# you cannot lower this version
# use application or software

kivy.require ( "1.9.1"

 
# Base class for your application inherits from the application class.
# app: always refers to ecze your app’s destination

from kivy.app import App 

 
# The widget is the basic building block
# GUI interfaces in Kivy.
# This provides a canvas that
# can be used to draw on screen.

from kivy.uix.widget import Widget

 
# From the graphics module we import
# Rectangle and color as they are
# Basic canvas construction.

from kivy.gra phics import Rectangle, Color

 

 
# the class in which we create the canvas

class CanvasWidget (Widget):

pass

 
# Create application class

class CanvasApp (App):

def build ( self ):

return CanvasWidget ()

 
# run application
CanvasApp (). run ()

# .kv canvas file

 
"CanvasWidget @ Widget" 

 

# create canvas

  canvas:

Color:

rgba: 0 , 0 , 1 , 1   # Blue

 

# canvas size and position

  Rectangle:

pos: self . pos  

size: self . size

Exit:

Now, how can we change the canvas color by any action, so below is an example where the screen color changes when clicked.

main.py file:

#import kivy module

import kivy 

 
# this limits the kivy version ie
# below this version you cannot
# use the application or software

kivy.require ( "1.9.1"

 
# Your application base class inherits from the application class.
# app: always refers to your application instance

from kivy.app import App 

 
# From the graphics module we import
# Rectangle and color as they are
# Basic canvas construction.

from kivy.graphics import Rectangle, Color

 
# Mixin class ButtonBehavior provides button behavior.

from kivy.uix.button import ButtonBehavior

 
# The "Label" widget is for rendering text.

from kivy.uix. label import Label

  
# the class in which we create the canvas

class CanvasWidget (ButtonBehavior, Label):

  pass

 
# Create application class

class CanvasApp (App):

def build ( self ):

return CanvasWidget ()

 
# run the application
CanvasApp (). run ()

.kv file:

# .kv canvas file

 
"CanvasWidget" 

 

# Create canvas

  canvas:

 

# Color blue if button is pressed

# otherwise the color is red

Color: 

rgb: ( 1 , 0 , 0 , 1 ) if self . state  = = ’normal’ else ( 0 , 0 , 1 , 1 )

 

# Canvas Rounded Rectangle

RoundedRectangle:

size: self . size

pos: self . pos

 

# Play with them if you want smooth corners for your button

radius: 100 , 100 , 100 , 100

 

 

  # Print text when touched or pressed

  on_release:

print ( " I have been clicked " )

Exit:

Notes:
The Kivy drawing instructions are not automatically related to the position or size of the widgets. Therefore, you need to consider these factors when drawing. To make your drawing instructions relative to the widget, the instructions must either be declared in KvLang or tied to the pos and size changes.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically