Snackbar¶
See also
Snackbars provide brief messages about app processes at the bottom of the screen.
Usage¶
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
#:import Snackbar kivymd.uix.snackbar.Snackbar
Screen:
MDRaisedButton:
text: "Create simple snackbar"
on_release: Snackbar(text="This is a snackbar!").show()
pos_hint: {"center_x": .5, "center_y": .5}
'''
class Test(MDApp):
def build(self):
return Builder.load_string(KV)
Test().run()
Usage with button¶
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
#:import Snackbar kivymd.uix.snackbar.Snackbar
Screen:
MDRaisedButton:
text: "Create simple snackbar"
pos_hint: {"center_x": .5, "center_y": .5}
on_release: Snackbar(text="This is a snackbar", button_text="BUTTON", button_callback=app.callback).show()
'''
class Test(MDApp):
def build(self):
return Builder.load_string(KV)
def callback(self, instance):
from kivymd.toast import toast
toast(instance.text)
Test().run()
Custom usage¶
from kivy.lang import Builder
from kivy.animation import Animation
from kivy.clock import Clock
from kivy.metrics import dp
from kivymd.app import MDApp
from kivymd.uix.snackbar import Snackbar
KV = '''
Screen:
MDFloatingActionButton:
id: button
x: root.width - self.width - dp(10)
y: dp(10)
on_release: app.snackbar_show()
'''
class Test(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.screen = Builder.load_string(KV)
self.snackbar = None
self._interval = 0
def build(self):
return self.screen
def wait_interval(self, interval):
self._interval += interval
if self._interval > self.snackbar.duration:
anim = Animation(y=dp(10), d=.2)
anim.start(self.screen.ids.button)
Clock.unschedule(self.wait_interval)
self._interval = 0
self.snackbar = None
def snackbar_show(self):
if not self.snackbar:
self.snackbar = Snackbar(text="This is a snackbar!")
self.snackbar.show()
anim = Animation(y=dp(72), d=.2)
anim.bind(on_complete=lambda *args: Clock.schedule_interval(
self.wait_interval, 0))
anim.start(self.screen.ids.button)
Test().run()
API - kivymd.uix.snackbar
¶
-
class
kivymd.uix.snackbar.
Snackbar
(**kwargs)¶ Bases:
kivy.uix.floatlayout.FloatLayout
-
text
¶ The text that will appear in the snackbar.
text
is aStringProperty
and defaults to ‘’.
-
font_size
¶ The font size of the text that will appear in the snackbar.
font_size
is aNumericProperty
and defaults to ’15sp’.
The text that will appear in the snackbar’s button.
Note
If this variable is None, the snackbar will have no button.
button_text
is aStringProperty
and defaults to ‘’.
The callback that will be triggered when the snackbar’s button is pressed.
Note
If this variable is None, the snackbar will have no button.
button_callback
is aObjectProperty
and defaults to None.
-
duration
¶ The amount of time that the snackbar will stay on screen for.
duration
is aNumericProperty
and defaults to 3.
-
show
(self)¶ Show the snackbar.
-