Swiper#
Warning The width of Warning The width of Use method
Usage#
MDSwiper:
MDSwiperItem:
MDSwiperItem:
MDSwiperItem:
Example#
from kivymd.app import MDApp
from kivy.lang.builder import Builder
kv = '''
<MySwiper@MDSwiperItem>
FitImage:
source: "guitar.png"
radius: [20,]
MDScreen:
MDTopAppBar:
id: toolbar
title: "MDSwiper"
elevation: 4
pos_hint: {"top": 1}
MDSwiper:
size_hint_y: None
height: root.height - toolbar.height - dp(40)
y: root.height - self.height - toolbar.height - dp(20)
MySwiper:
MySwiper:
MySwiper:
MySwiper:
MySwiper:
'''
class Main(MDApp):
def build(self):
return Builder.load_string(kv)
Main().run()
MDSwiperItem
is adjusted automatically. Consider changing
that by width_mult
.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")
Example#
from kivy.lang.builder import Builder
from kivymd.app import MDApp
kv = '''
<MagicButton@MagicBehavior+MDIconButton>
<MySwiper@MDSwiperItem>
RelativeLayout:
FitImage:
source: "guitar.png"
radius: [20,]
MDBoxLayout:
adaptive_height: True
spacing: "12dp"
MagicButton:
id: icon
icon: "weather-sunny"
user_font_size: "56sp"
opposite_colors: True
MDLabel:
text: "MDLabel"
font_style: "H5"
size_hint_y: None
height: self.texture_size[1]
pos_hint: {"center_y": .5}
opposite_colors: True
MDScreen:
MDTopAppBar:
id: toolbar
title: "MDSwiper"
elevation: 4
pos_hint: {"top": 1}
MDSwiper:
size_hint_y: None
height: root.height - toolbar.height - dp(40)
y: root.height - self.height - toolbar.height - dp(20)
on_swipe: self.get_current_item().ids.icon.shake()
MySwiper:
MySwiper:
MySwiper:
MySwiper:
MySwiper:
'''
class Main(MDApp):
def build(self):
return Builder.load_string(kv)
Main().run()
How to automatically switch a SwiperItem?#
set_current
which takes the index of MDSwiperItem
as argument.Example#
MDSwiper:
id: swiper
MDSwiperItem: # First widget with index 0
MDSwiperItem: # Second widget with index 1
MDRaisedButton:
text: "Go to Second"
on_release: swiper.set_current(1)
API - kivymd.uix.swiper.swiper
#
- class kivymd.uix.swiper.swiper.MDSwiperItem(*args, **kwargs)#
MDSwiperItem
is aBoxLayout
but it’s size is adjusted automatically.
- class kivymd.uix.swiper.swiper.MDSwiper(*args, **kwargs)#
ScrollView class. For more information, see in the
ScrollView
class documentation.- items_spacing#
The space between each
MDSwiperItem
.items_spacing
is anNumericProperty
and defaults to 20dp.
- transition_duration#
Duration of switching between
MDSwiperItem
.transition_duration
is anNumericProperty
and defaults to 0.2.
- size_duration#
Duration of changing the size of
MDSwiperItem
.transition_duration
is anNumericProperty
and defaults to 0.2.
- size_transition#
The type of animation used for changing the size of
MDSwiperItem
.size_transition
is anStringProperty
and defaults to out_quad.
- swipe_transition#
The type of animation used for swiping.
swipe_transition
is anStringProperty
and defaults to out_quad.
- swipe_distance#
Distance to move before swiping the
MDSwiperItem
.swipe_distance
is anNumericProperty
and defaults to 70dp.
- width_mult#
This number is multiplied by
items_spacing
x2 and then subtracted from the width of window to specify the width ofMDSwiperItem
. So by decreasing thewidth_mult
the width ofMDSwiperItem
increases and vice versa.width_mult
is anNumericProperty
and defaults to 3.
- swipe_on_scroll#
Wheter to swipe on mouse wheel scrolling or not.
swipe_on_scroll
is anBooleanProperty
and defaults to True.
- add_widget(self, 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.
New 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.
New 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(self, 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(self, index)#
Switch to given
MDSwiperItem
index.
- get_current_index(self)#
Returns the current
MDSwiperItem
index.
- get_current_item(self)#
Returns the current
MDSwiperItem
instance.
- get_items(self)#
Returns the list of
MDSwiperItem
children.Note
Use get_items() to get the list of children instead of MDSwiper.children.
- on_swipe(self)#
- on_pre_swipe(self)#
- on_overswipe_right(self)#
- on_overswipe_left(self)#
- on_swipe_left(self)#
- on_swipe_right(self)#
- swipe_left(self)#
- swipe_right(self)#
- on_scroll_start(self, touch, check_children=True)#
- on_touch_down(self, touch)#
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.
- 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(self, touch)#
Receive a touch up event. The touch is in parent coordinates.
See
on_touch_down()
for more information.