Kivy - Utils



The "kivy.utils" module in Kivy library is a collection of general utility functions in various categories such as maths, color, algebraic function etc.

QueryDict

The object of QueryDict class is similar to Python's built-in dict class. Additionally it has a provision to query the object with dot ( . ) operator.

To construct QueryDict, you can either pass a list of two element tuples, or a dict object itself.

# list of tuples
qd = QueryDict([('a',1), ('b',2)])
print (qd)

Each tuple element of the list should have two items. First item is the key, and the second item is its value.

{'a': 1, 'b': 2}

On the other hand, you can pass a dict object itself to QueryDict constructor.

qd=QueryDict({'a':1, 'b':2})

While the value belonging to a certain key can be fetched with the [] operator defined in the standard dict, the QueryDict provides a dot operator. Hence, "qd.k" is same as "qd['k']". Note that the get() method od dict class can also be used with QueryDict.

You can update the value of a key using the conventional slice operator assignment or the dot operator.

qd.a=100
qd['b']=200

Try out the example below −

from kivy.utils import *

# list of tuples
qd=QueryDict([('a',1), ('b',2)])
print (qd)
print (qd.a, qd['a'])

qd=QueryDict({'a':1, 'b':2})
print (qd)
print (qd.b, qd['b'])
print (qd.get('a'))

qd.a=100
qd['b']=200
print (qd)

SafeList

The SafeList class in Kivy inherits the built-in list class. In addition to the methods inherited from list, a new method − clear() is defined in SafeList class. It removes all items in the list.

You can pass a mutable sequence (list) to the constructor to create a SafeList object. If no argument is passed, it creates an empty list. Calling clear() method removes all items.

Example

from kivy.utils import *

sl = SafeList([1,2,3,4])
print ("SafeList:",sl)

l = [1,2,3,4]
sl = SafeList(l)
print ("SafeList:",sl)

sl.clear()
print ("SafeList:",sl)

Output

SafeList: [1, 2, 3, 4]
SafeList: [1, 2, 3, 4]
SafeList: []

difference()

This function returns the difference between two lists. More specifically, it removes those items from the first list that are found in the second list.

Example

from kivy.utils import *

l1=[1,2,3,4]
l2=[3,4,5,6]
print (l1, l2)
print ("l1-l2:",difference(l1,l2))
print ("l2-l1:",difference(l2,l1))

Output

[1, 2, 3, 4] [3, 4, 5, 6]
l1-l2: [1, 2]
l2-l1: [5, 6]

escape_markup()

A Label on Kivy app window is capable of displaying a markup text. However, if you want effect of the markup symbols not to take effect, you can escape markup characters found in the text. This is intended to be used when markup text is activated on the Label.

In the example below, the text to be displayed on the label contains [b] and [/b] markup tags, which would convert the test to bold. However, to ignore this effect, the text is passed to escape_markup() function.

Example

from kivy.app import App
from kivy.uix.label import Label
from kivy.utils import escape_markup
from kivy.core.window import Window

Window.size = (720,400)

class HelloApp(App):
   def build(self):
      text = 'This is an [b]important[/b] message'
      text = '[color=ff0000]' + escape_markup(text) + '[/color]'
      lbl=Label(text=text, font_size=40, markup=True)
      return lbl

HelloApp().run()

Output

Kivy Utils

get_color_from_hex()

Transform a hex string color to a kivy Color. The RGBA values for color property are given to be between 0 to 1. Since RGB values range from 0 to 255, the Kivy Color values divide the number by 255. Hence RGB values 50, 100, 200 are represented as 50/255, 100/255 and 200/255 respectively.

The hex color values are given as a string with 2 hexadecimal numbers each for RGB and prefixed by the "#" symbol. The get_color_from_hex() function converts a Hex string to Kivy color values.

Example

from kivy.utils import *
c = get_color_from_hex("#00ff00")
print (c)

Output

[0.0, 1.0, 0.0, 1.0]

get_hex_from_color(color)

Transform a kivy Color to a hex value −

Example

from kivy.utils import *
c = get_hex_from_color([0,1,0,1])
print (c)

Output

#00ff00ff

rgba()

returns a Kivy color (4 value from 0-1 range) from either a hex string or a list of 0-255 values.

Example

from kivy.utils import *

# from RGBA color values
c = rgba([100,150,200, 255])
print ("from RGBA:",c)

# from hex string
c = rgba('#3fc4e57f')
print ("from hex string:",c)

Output

from RGBA: [0.39215686274509803, 0.5882352941176471, 0.7843137254901961, 1.0]
from hex string: [0.24705882352941178, 0.7686274509803922, 0.8980392156862745, 0.4980392156862745]
Advertisements