Quick search

Table Of Contents

Widget class

The Widget class is the base class required to create a Widget. This widget class is designed with a couple of principles in mind:

Event Driven
The widget interaction is built on top of events that occur. If a property changes, the widget can do something. If nothing changes in the widget, nothing will be done. That’s the main goal of the Property class.
Separate the widget and its graphical representation
Widgets don’t have a draw() method. This is done on purpose: The idea is to allow you to create your own graphical representation outside the widget class. Obviously you can still use all the available properties to do that, so that your representation properly reflects the widget’s current state. Every widget has its own Canvas that you can use to draw. This separation allows Kivy to run your application in a very efficient manner.
Bounding Box / Collision
Often you want to know if a certain point is within the bounds of your widget. An example would be a button widget where you want to only trigger an action when the button itself is actually touched. For this, you can use the Widget.collide_point() method, which will return True if the point you pass it is inside the axis-aligned bounding box defined by the widget’s position and size. If a simple AABB is not sufficient, you can override the method to perform the collision checks with more complex shapes, e.g., a polygon. You can also check if a widget collides with another widget with Widget.collide_widget().

We also have some default values and behaviors that you should be aware of:

  • A Widget is not a Layout: it will not change the position or the size of its children. If you want control over positioning or sizing, use a Layout.
  • The default size of a widget is (100, 100). This is only changed if the parent is a Layout. For example, if you add a Label inside a Button, the label will not inherit the buttons size or position because the button is not a Layout: it’s just another Widget.
  • The default size_hint is (1, 1). If the parent is a Layout, then the widget size will be the parent/layout size.
  • Widget.on_touch_down(), Widget.on_touch_move(), Widget.on_touch_up() don’t do any sort of collisions. If you want to know if the touch is inside your widget, use Widget.collide_point().

Using Properties

When you read the documentation, all properties are described in the format:

<name> is a <property class> and defaults to <default value>.

e.g.

text is a StringProperty and defaults to ‘’.

If you want to be notified when the pos attribute changes, i.e. when the widget moves, you can bind your own callback function like this:

def callback_pos(instance, value):
    print('The widget', instance, 'moved to', value)

wid = Widget()
wid.bind(pos=callback_pos)

Read more about the Properties.

class kivy.uix.widget.Widget(**kwargs)[source]

Bases: kivy.uix.widget.WidgetBase

Widget class. See module documentation for more information.

Events:
on_touch_down:

Fired when a new touch event occurs

on_touch_move:

Fired when an existing touch moves

on_touch_up:

Fired when an existing touch disappears

Changed in version 1.0.9: Everything related to event properties has been moved to the EventDispatcher. Event properties can now be used when contructing a simple class without subclassing Widget.

Changed in version 1.5.0: Constructor now accept on_* arguments to automatically bind callbacks to properties or events, as the Kv language.

add_widget(widget, index=0)[source]

Add a new widget as a child of this widget.

Parameters:
widget: Widget

Widget to add to our list of children.

index: int, defaults to 0

(this attribute was added in 1.0.5) Index to insert the widget in the list

>>> from kivy.uix.button import Button
>>> from kivy.uix.slider import Slider
>>> root = Widget()
>>> root.add_widget(Button())
>>> slider = Slider()
>>> root.add_widget(slider)
canvas = None

Canvas of the widget.

The canvas is a graphics object that contains all the drawing instructions for the graphical representation of the widget.

There are no general properties for the Widget class, such as background color, to keep the design simple and lean. Some derived classes, such as Button, do add such convenience properties but generally the developer is responsible for implementing the graphics representation for a custom widget from the ground up. See the derived widget classes for patterns to follow and extend.

See Canvas for more information about the usage.

center

Center position of the widget.

center is a ReferenceListProperty of (center_x, center_y)

center_x

X center position of the widget.

center_x is an AliasProperty of (x + width / 2.),

center_y

Y center position of the widget.

center_y is an AliasProperty of (y + height / 2.)

children

List of children of this widget.

children is a ListProperty and defaults to an empty list.

Use add_widget() and remove_widget() for manipulating the children list. Don’t manipulate the children list directly unless you know what you are doing.

clear_widgets(children=None)[source]

Remove all widgets added to this widget.

Changed in version 1.8.0: children argument can be used to select the children we want to remove. It should be a list of children (or filtered list) of the current widget.

cls

Class of the widget, used for styling.

collide_point(x, y)[source]

Check if a point (x, y) is inside the widget’s axis aligned bounding box.

Parameters:
x: numeric

X position of the point (in window coordinates)

y: numeric

Y position of the point (in window coordinates)

Returns:

bool, True if the point is inside the bounding box.

>>> Widget(pos=(10, 10), size=(50, 50)).collide_point(40, 40)
True
collide_widget(wid)[source]

Check if the other widget collides with this widget. Performs an axis-aligned bounding box intersection test by default.

Parameters:
wid: Widget class

Widget to collide with.

Returns:

bool, True if the other widget collides with this widget.

>>> wid = Widget(size=(50, 50))
>>> wid2 = Widget(size=(50, 50), pos=(25, 25))
>>> wid.collide_widget(wid2)
True
>>> wid2.pos = (55, 55)
>>> wid.collide_widget(wid2)
False
disabled

Indicates whether this widget can interact with input or not.

Note

1. Child Widgets, when added to a disabled widget, will be disabled automatically, 2. Disabling/enabling a parent disables/enables all it’s children.

New in version 1.8.0.

disabled is a BooleanProperty and defaults to False.

get_parent_window()[source]

Return the parent window.

Returns:Instance of the parent window. Can be a WindowBase or Widget.
get_root_window()[source]

Return the root window.

Returns:Instance of the root window. Can be a WindowBase or Widget.
height

Height of the widget.

height is a NumericProperty and defaults to 100.

Warning

Keep in mind that the height property is subject to layout logic and that this has not yet happened at the time of the widget’s __init__ method.

id

Unique identifier of the widget in the tree.

id is a StringProperty and defaults to None.

Warning

If the id is already used in the tree, an exception will be raised.

ids

This is a Dictionary of id’s defined in your kv language. This will only be populated if you use id’s in your kv language code.

New in version 1.7.0.

ids is a DictProperty and defaults to a empty dict {}.

on_touch_down(touch)[source]

Receive a touch down event.

Parameters:
touch: MotionEvent class

Touch received. The touch is in parent coordinates. See relativelayout for a discussion on coordinate systems.

Returns:

bool. If True, the dispatching of the touch event will stop.

on_touch_move(touch)[source]

Receive a touch move event. The touch is in parent coordinates.

See on_touch_down() for more information.

on_touch_up(touch)[source]

Receive a touch up event. The touch is in parent coordinates.

See on_touch_down() for more information.

opacity

Opacity of the widget and all the children.

New in version 1.4.1.

The opacity attribute controls the opacity of the widget and its children. Be careful, it’s a cumulative attribute: the value is multiplied by the current global opacity and the result is applied to the current context color.

For example, if the parent has an opacity of 0.5 and a child has an opacity of 0.2, the real opacity of the child will be 0.5 * 0.2 = 0.1.

Then, the opacity is applied by the shader as:

frag_color = color * vec4(1.0, 1.0, 1.0, opacity);

opacity is a NumericProperty and defaults to 1.0.

parent

Parent of this widget.

parent is an ObjectProperty and defaults to None.

The parent of a widget is set when the widget is added to another widget and unset when the widget is removed from its parent.

pos

Position of the widget.

pos is a ReferenceListProperty of (x, y) properties.

pos_hint

Position hint. This property allows you to set the position of the widget inside its parent layout, in percent (similar to size_hint).

For example, if you want to set the top of the widget to be at 90% height of its parent layout, you can write:

widget = Widget(pos_hint={‘top’: 0.9})

The keys ‘x’, ‘right’ and ‘center_x’ will use the parent width. The keys ‘y’, ‘top’ and ‘center_y’ will use the parent height.

See Float Layout for further reference.

Position hint is only used by the FloatLayout and Window.

pos_hint is an ObjectProperty containing a dict.

proxy_ref[source]

Return a proxy reference to the widget, i.e. without creating a reference to the widget. See weakref.proxy for more information.

New in version 1.7.2.

remove_widget(widget)[source]

Remove a widget from the children of this widget.

Parameters:
widget: Widget

Widget to remove from our children list.

>>> from kivy.uix.button import Button
>>> root = Widget()
>>> button = Button()
>>> root.add_widget(button)
>>> root.remove_widget(button)
right

Right position of the widget.

right is an AliasProperty of (x + width),

size

Size of the widget.

size is a ReferenceListProperty of (width, height) properties.

size_hint

Size hint.

size_hint is a ReferenceListProperty of (size_hint_x, size_hint_y).

See size_hint_x for more information

size_hint_x

X size hint. Represents how much space the widget should use in the direction of the X axis relative to its parent’s width. Only the Layout and Window classes make use of the hint.

The value is in percent as a float from 0. to 1., where 1. means the full size of his parent. 0.5 represents 50%.

size_hint_x is a NumericProperty and defaults to 1.

size_hint_y

Y size hint.

size_hint_y is a NumericProperty and defaults to 1.

See size_hint_x for more information

to_local(x, y, relative=False)[source]

Transform parent coordinates to local coordinates. See relativelayout for details on the coordinate systems.

Parameters:
relative: bool, defaults to False

Change to True if you want to translate coordinates to relative widget coordinates.

to_parent(x, y, relative=False)[source]

Transform local coordinates to parent coordinates. See relativelayout for details on the coordinate systems.

Parameters:
relative: bool, defaults to False

Change to True if you want to translate relative positions from a widget to its parent coordinates.

to_widget(x, y, relative=False)[source]

Convert the given coordinate from window to local widget coordinates. See relativelayout for details on the coordinate systems.

to_window(x, y, initial=True, relative=False)[source]

Transform local coordinates to window coordinates. See relativelayout for details on the coordinate systems.

top

Top position of the widget.

top is an AliasProperty of (y + height),

width

Width of the widget.

width is a NumericProperty ans defaults to 100.

Warning

Keep in mind that the width property is subject to layout logic and that this has not yet happened at the time of the widget’s __init__ method.

x

X position of the widget.

x is a NumericProperty and defaults to 0.

y

Y position of the widget.

y is a NumericProperty and defaults to 0.

class kivy.uix.widget.WidgetException[source]

Bases: exceptions.Exception

Fired when the widget gets an exception.