Swiper#
#
Usage#
MDSwiper:
MDSwiperItem:
MDSwiperItem:
MDSwiperItem:
MDSwiper(
MDSwiperItem(),
MDSwiperItem(),
MDSwiperItem(),
)
Example#
from kivy.lang.builder import Builder
from kivymd.app import MDApp
kv = '''
<MySwiper@MDSwiperItem>
FitImage:
source: "bg.jpg"
radius: [dp(20),]
MDScreen:
md_bg_color: self.theme_cls.backgroundColor
MDSwiper:
size_hint_y: None
height: root.height - dp(40)
y: root.height - self.height - dp(20)
MySwiper:
MySwiper:
MySwiper:
MySwiper:
MySwiper:
'''
class Main(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
return Builder.load_string(kv)
Main().run()
from kivymd.material_resources import dp
from kivymd.app import MDApp
from kivymd.uix.fitimage import FitImage
from kivymd.uix.screen import MDScreen
from kivymd.uix.swiper import MDSwiper, MDSwiperItem
class MySwiper(MDSwiperItem):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.widgets = [
FitImage(
source="bg.jpg",
radius=[dp(20), ],
)
]
class Main(MDApp):
def on_start(self):
swiper = self.root.get_ids().swiper
swiper.height = (self.root.height - dp(40))
swiper.y = (self.root.height - swiper.height - dp(20))
def build(self):
self.theme_cls.theme_style = "Dark"
return (
MDScreen(
MDSwiper(
MySwiper(),
MySwiper(),
MySwiper(),
MySwiper(),
MySwiper(),
size_hint_y=None,
id="swiper",
),
md_bg_color=self.theme_cls.backgroundColor,
)
)
Main().run()
Warning
The width of MDSwiperItem is adjusted automatically. Consider changing
that by width_mult.
Warning
The width of MDSwiper is automatically adjusted according to the width of the window.
MDSwiper provides the following events for use:
__events__ = (
"on_swipe",
"on_pre_swipe",
"on_overswipe_right",
"on_overswipe_left",
"on_swipe_left",
"on_swipe_right"
)
MDSwiper:
on_swipe: print("on_swipe")
on_pre_swipe: print("on_pre_swipe")
on_overswipe_right: print("on_overswipe_right")
on_overswipe_left: print("on_overswipe_left")
on_swipe_left: print("on_swipe_left")
on_swipe_right: print("on_swipe_right")
API - kivymd.uix.swiper.swiper#
- class kivymd.uix.swiper.swiper.MDSwiperItem(*args, **kwargs)#
Swiper item class.
For more information, see in the
MDBoxLayoutclass documentation.
- class kivymd.uix.swiper.swiper.MDSwiper(*args, **kwargs)#
Swiper class.
For more information, see in the
ScrollViewclass documentation.- items_spacing#
The space between each
MDSwiperItem.items_spacingis anNumericPropertyand defaults to 20dp.
- transition_duration#
Duration of switching between
MDSwiperItem.transition_durationis anNumericPropertyand defaults to 0.2.
- size_duration#
Duration of changing the size of
MDSwiperItem.transition_durationis anNumericPropertyand defaults to 0.2.
- size_transition#
The type of animation used for changing the size of
MDSwiperItem.size_transitionis anStringPropertyand defaults to out_quad.
- swipe_transition#
The type of animation used for swiping.
swipe_transitionis anStringPropertyand defaults to out_quad.
- swipe_distance#
Distance to move before swiping the
MDSwiperItem.swipe_distanceis anNumericPropertyand defaults to 70dp.
- width_mult#
This number is multiplied by
items_spacingx2 and then subtracted from the width of window to specify the width ofMDSwiperItem. So by decreasing thewidth_multthe width ofMDSwiperItemincreases and vice versa.width_multis anNumericPropertyand defaults to 3.
- swipe_on_scroll#
Wheter to swipe on mouse wheel scrolling or not.
swipe_on_scrollis anBooleanPropertyand defaults to True.
- add_widget(widget, index=0)#
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
Index to insert the widget in the list. Notice that the default of 0 means the widget is inserted at the beginning of the list and will thus be drawn on top of other sibling widgets. For a full discussion of the index and widget hierarchy, please see the Widgets Programming Guide.
Added in version 1.0.5.
- canvas: str, defaults to None
Canvas to add widget’s canvas to. Can be ‘before’, ‘after’ or None for the default canvas.
Added in version 1.9.0.
- widget:
>>> from kivy.uix.button import Button >>> from kivy.uix.slider import Slider >>> root = Widget() >>> root.add_widget(Button()) >>> slider = Slider() >>> root.add_widget(slider)
- remove_widget(widget)#
Remove a widget from the children of this widget.
- Parameters:
- widget:
Widget Widget to remove from our children list.
- widget:
>>> from kivy.uix.button import Button >>> root = Widget() >>> button = Button() >>> root.add_widget(button) >>> root.remove_widget(button)
- set_current(index)#
Switch to given
MDSwiperItemindex.
- get_current_index()#
Returns the current
MDSwiperItemindex.
- get_current_item()#
Returns the current
MDSwiperIteminstance.
- get_items()#
Returns the list of
MDSwiperItemchildren.Note
Use get_items() to get the list of children instead of MDSwiper.children.
- on_swipe()#
- on_pre_swipe()#
- on_overswipe_right()#
- on_overswipe_left()#
- on_swipe_left()#
- on_swipe_right()#
- swipe_left()#
- swipe_right()#
- on_scroll_start(touch, check_children=True)#
- on_touch_down(touch)#
Receive a touch down event.
- Parameters:
- touch:
MotionEventclass Touch received. The touch is in parent coordinates. See
relativelayoutfor a discussion on coordinate systems.
- touch:
- Returns:
bool If True, the dispatching of the touch event will stop. If False, the event will continue to be dispatched to the rest of the widget tree.
- on_touch_up(touch)#
Receive a touch up event. The touch is in parent coordinates.
See
on_touch_down()for more information.