Carousel - Add widget on swipe

125 views
Skip to first unread message

John Boyd

unread,
Oct 18, 2015, 10:48:15 AM10/18/15
to Kivy users support
Hello,

I overrode the (private)_start_animation function to be able to add widget to Carousel on swipe.

Is there any other/better way to do this ?
Would this cause any problems ?

from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.uix.label import Label


import time


class Pages(Carousel):
   
def __init__(self, **kwargs):
       
super(Pages,self).__init__(**kwargs)
       
   
def _start_animation(self, *args, **kwargs):
       
if self.next_slide == None:
           
self.add_widget(Label(text=str(time.time())))
           
super(Pages, self)._start_animation(*args)
       
else:
           
super(Pages, self)._start_animation(*args)
           


class ScrapApp(App):
   
def build(self):
       
return Pages()




if __name__ == '__main__':
   
ScrapApp().run()








swiped.py

Samuel Loury

unread,
Oct 19, 2015, 3:46:36 PM10/19/15
to John Boyd, Kivy users support
Hi,
John Boyd <jbo...@gmail.com> writes:

> I overrode the (private)_start_animation function to be able to add widget
> to Carousel on swipe.

In order to add a widget on swipe, you could bind on the "on_index"
event.

--8<---------------cut here---------------start------------->8---
class MYCarousel(Carousel):
def on_index(self, inst, pos):
super(MyCarousel, self).on_index(inst, pos)
...
--8<---------------cut here---------------end--------------->8---


> Is there any other/better way to do this ?
> Would this cause any problems ?

I can show another situation where overriding the _start_animation was
needed.

If you want to bind the swipe event on the keyboard, you will probably
want to prevent the swipe event to occur went one is already being
done. To do that, I changed it to :

--8<---------------cut here---------------start------------->8---
def _start_animation(self, *args, **kwargs):
# compute target offset for ease back, next or prev
new_offset = 0
direction = kwargs.get('direction', self.direction)
is_horizontal = direction[0] in ['r', 'l']
extent = self.width if is_horizontal else self.height
min_move = kwargs.get('min_move', self.min_move)
_offset = kwargs.get('offset', self._offset)

if _offset < min_move * -extent:
new_offset = -extent
elif _offset > min_move * extent:
new_offset = extent

# if new_offset is 0, it wasnt enough to go next/prev
dur = self.anim_move_duration
if new_offset == 0:
dur = self.anim_cancel_duration

# detect edge cases if not looping
len_slides = len(self.slides)
index = self.index
if not self.loop or len_slides == 1:
is_first = (index == 0)
is_last = (index == len_slides - 1)
if direction[0] in ['r', 't']:
towards_prev = (new_offset > 0)
towards_next = (new_offset < 0)
else:
towards_prev = (new_offset < 0)
towards_next = (new_offset > 0)
if (is_first and towards_prev) or (is_last and towards_next):
new_offset = 0

anim = Animation(_offset=new_offset, d=dur, t=self.anim_type)
anim.cancel_all(self)

def _cmp(*l):
if self._skip_slide is not None:
self.index = self._skip_slide
self._skip_slide = None
self.in_anim = False

self.in_anim = True
anim.bind(on_complete=_cmp)
anim.start(self)
--8<---------------cut here---------------end--------------->8---

This way, I could run:

--8<---------------cut here---------------start------------->8---
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] in ["right", "left"] and self.in_anim:
return False
if keycode[1] == 'right':
self.load_next()
elif keycode[1] == 'left':
self.load_previous()
else:
return False
return True
--8<---------------cut here---------------end--------------->8---

My two cents!

--
Konubinix
GPG Key : 7439106A
Fingerprint: 5993 BE7A DA65 E2D9 06CE 5C36 75D2 3CED 7439 106A
signature.asc

John Boyd

unread,
Oct 19, 2015, 7:26:21 PM10/19/15
to Kivy users support
Hello Samuel,

Thanks for your suggestions.

The on_index override worked, however the Carousel had to have at least
one slide already existing as opposed to my _start_animation override.

With regard to your _start_animation override. I was not really interested in anything dealing with the keyboard,
but now you have given me something else to consider : )

Best Regards.
Reply all
Reply to author
Forward
0 new messages