Quick search

Table Of Contents

Utils

The Utils module provides a selection of general utility functions and classes that may be useful for various applications. These include maths, color, algebraic and platform functions.

バージョン 1.6.0 で変更: The OrderedDict class has been removed. Use collections.OrderedDict instead.

kivy.utils.intersection(set1, set2)[ソース]

Return the intersection of 2 lists.

kivy.utils.difference(set1, set2)[ソース]

Return the difference between 2 lists.

kivy.utils.strtotuple(s)[ソース]

Convert a tuple string into a tuple with some security checks. Designed to be used with the eval() function:

a = (12, 54, 68)
b = str(a)         # return '(12, 54, 68)'
c = strtotuple(b)  # return (12, 54, 68)
kivy.utils.get_color_from_hex(s)[ソース]

Transform a hex string color to a kivy Color.

kivy.utils.get_hex_from_color(color)[ソース]

Transform a kivy Color to a hex value:

>>> get_hex_from_color((0, 1, 0))
'#00ff00'
>>> get_hex_from_color((.25, .77, .90, .5))
'#3fc4e57f'

バージョン 1.5.0 で追加.

kivy.utils.get_random_color(alpha=1.0)[ソース]

Returns a random color (4 tuple).

Parameters:
alpha: float, defaults to 1.0

If alpha == ‘random’, a random alpha value is generated.

kivy.utils.is_color_transparent(c)[ソース]

Return True if the alpha channel is 0.

kivy.utils.boundary(value, minvalue, maxvalue)[ソース]

Limit a value between a minvalue and maxvalue.

kivy.utils.deprecated(func)[ソース]

This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emitted the first time the function is used.

class kivy.utils.SafeList[ソース]

ベースクラス: builtins.list

List with a clear() method.

警告

Usage of the iterate() function will decrease your performance.

kivy.utils.interpolate(value_from, value_to, step=10)[ソース]

Interpolate between two values. This can be useful for smoothing some transitions. For example:

# instead of setting directly
self.pos = pos

# use interpolate, and you'll have a nicer transition
self.pos = interpolate(self.pos, new_pos)

警告

These interpolations work only on lists/tuples/doubles with the same dimensions. No test is done to check the dimensions are the same.

class kivy.utils.QueryDict[ソース]

ベースクラス: builtins.dict

QueryDict is a dict() that can be queried with dot.

d = QueryDict()
# create a key named toto, with the value 1
d.toto = 1
# it's the same as
d['toto'] = 1

バージョン 1.0.4 で追加.

kivy.utils.platform = 'win'

A string identifying the current operating system. It is one of: ‘win’, ‘linux’, ‘android’, ‘macosx’, ‘ios’ or ‘unknown’. You can use it as follows:

from kivy.utils import platform
if platform == 'linux':
    do_linux_things()

バージョン 1.3.0 で追加.

バージョン 1.8.0 で変更: platform is now a variable instead of a function.

kivy.utils.escape_markup(text)[ソース]

Escape markup characters found in the text. Intended to be used when markup text is activated on the Label:

untrusted_text = escape_markup('Look at the example [1]')
text = '[color=ff0000]' + untrusted_text + '[/color]'
w = Label(text=text, markup=True)

バージョン 1.3.0 で追加.

class kivy.utils.reify(func)[ソース]

ベースクラス: builtins.object

Put the result of a method which uses this (non-data) descriptor decorator in the instance dict after the first call, effectively replacing the decorator with an instance variable.

It acts like @property, except that the function is only ever called once; after that, the value is cached as a regular attribute. This gives you lazy attribute creation on objects that are meant to be immutable.

Taken from the Pyramid project.

To use this as a decorator:

@reify
def lazy(self):
     ...
     return hard_to_compute_int
first_time = self.lazy   # lazy is reify obj, reify.__get__() runs
second_time = self.lazy  # lazy is hard_to_compute_int
kivy.utils.rgba(s, *args)[ソース]

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

バージョン 1.10.0 で追加.