doc: neatened example, reverted unnecessary additions/changes

This commit is contained in:
Zen-CODE 2014-05-24 20:04:06 +02:00
parent 028fc4cf14
commit 410b33f9e0
1 changed files with 12 additions and 19 deletions

View File

@ -326,26 +326,21 @@ In Python::
pos=layout_instance.pos) pos=layout_instance.pos)
Unfortunately, this will only draw a rectangle at the layout's initial position Unfortunately, this will only draw a rectangle at the layout's initial position
and size. To make sure the rect is drawn inside the layout, when layout size/pos and size. To make sure the rect is drawn inside the layout, when the layout
changes, we need to listen to any changes and update the rectangles size and pos. size/pos changes, we need to listen to any changes and update the rectangles
size and pos. We can do that as follows::
Here is an example that paints a green background for a FloatLayout. In Python, with layout_instance.canvas.before:
we need to explicitely handle pos and size changes which the kv does Color(0, 1, 0, 1) # green; colors range from 0-1 instead of 0-255
automatically:: self.rect = Rectangle(size=layout_instance.size,
pos=layout_instance.pos)
def draw_green(instance, value): def update_rect(instance, value):
# If we have already created a rect, change it, else create and attach it
if hasattr(instance, "rect"):
instance.rect.pos = instance.pos instance.rect.pos = instance.pos
instance.rect.size = instance.size instance.rect.size = instance.size
else:
with instance.canvas.before:
Color(0, 1, 0, 1)
instance.rect = Rectangle(size=instance.size,
pos=instance.pos)
# listen to size and position changes # listen to size and position changes
self.bind(pos=draw_green, size=draw_green) layout_instance.bind(pos=update_rect, size=update_rect)
In kv: In kv:
@ -364,9 +359,7 @@ The kv declaration sets an implicit binding: the last two kv lines ensure that
the |pos| and |size| values of the rectangle will update when the |pos| of the the |pos| and |size| values of the rectangle will update when the |pos| of the
|FloatLayout| changes. |FloatLayout| changes.
Now we put the snippets above into the shell of Kivy App. In the Python example Now we put the snippets above into the shell of Kivy App.
above, each pos/size change results in a "hasattr" call. Here we provide a more
optimal example using a dedicated "_update_rect" callback.
Pure Python way:: Pure Python way::