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
MDScreen:
MDRaisedButton:
text: "Create simple snackbar"
on_release: Snackbar(text="This is a snackbar!").open()
pos_hint: {"center_x": .5, "center_y": .5}
'''
class Test(MDApp):
def build(self):
return Builder.load_string(KV)
Test().run()
Usage with snackbar_x, snackbar_y#
Snackbar(
text="This is a snackbar!",
snackbar_x="10dp",
snackbar_y="10dp",
size_hint_x=(
Window.width - (dp(10) * 2)
) / Window.width
).open()
Control width#
Snackbar(
text="This is a snackbar!",
snackbar_x="10dp",
snackbar_y="10dp",
size_hint_x=.5
).open()
Custom text color#
Snackbar(
text="[color=#ddbb34]This is a snackbar![/color]",
snackbar_y="10dp",
snackbar_y="10dp",
size_hint_x=.7
).open()
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 = '''
MDScreen:
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 + 0.5:
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.open()
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()
Custom Snackbar#
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.properties import StringProperty, NumericProperty
from kivymd.app import MDApp
from kivymd.uix.button import MDFlatButton
from kivymd.uix.snackbar import BaseSnackbar
KV = '''
<CustomSnackbar>
MDIconButton:
pos_hint: {'center_y': .5}
icon: root.icon
opposite_colors: True
MDLabel:
id: text_bar
size_hint_y: None
height: self.texture_size[1]
text: root.text
font_size: root.font_size
theme_text_color: 'Custom'
text_color: 'ffffff'
shorten: True
shorten_from: 'right'
pos_hint: {'center_y': .5}
MDScreen:
MDRaisedButton:
text: "SHOW"
pos_hint: {"center_x": .5, "center_y": .45}
on_press: app.show()
'''
class CustomSnackbar(BaseSnackbar):
text = StringProperty(None)
icon = StringProperty(None)
font_size = NumericProperty("15sp")
class Test(MDApp):
def build(self):
return Builder.load_string(KV)
def show(self):
snackbar = CustomSnackbar(
text="This is a snackbar!",
icon="information",
snackbar_x="10dp",
snackbar_y="10dp",
buttons=[MDFlatButton(text="ACTION", text_color=(1, 1, 1, 1))]
)
snackbar.size_hint_x = (
Window.width - (snackbar.snackbar_x * 2)
) / Window.width
snackbar.open()
Test().run()
API - kivymd.uix.snackbar.snackbar#
- class kivymd.uix.snackbar.snackbar.BaseSnackbar(**kwargs)#
- Events:
on_openCalled when a dialog is opened.
on_dismissWhen the front layer rises.
Abstract base class for all Snackbars. This class handles sizing, positioning, shape and events for Snackbars
All Snackbars will be made off of this BaseSnackbar.
BaseSnackbar will always try to fill the remainder of the screen with your Snackbar.
To make your Snackbar dynamic and symetric with snackbar_x.
Set size_hint_x like below:
size_hint_z = ( Window.width - (snackbar_x * 2) ) / Window.width
- duration#
The amount of time that the snackbar will stay on screen for.
durationis aNumericPropertyand defaults to 3.
- auto_dismiss#
Whether to use automatic closing of the snackbar or not.
auto_dismissis aBooleanPropertyand defaults to ‘True’.
- bg_color#
Snackbar background.
bg_coloris aColorPropertyand defaults to None.
- buttons#
Snackbar buttons.
buttonsis aListPropertyand defaults to ‘[]’
- radius#
Snackbar radius.
radiusis aListPropertyand defaults to ‘[5, 5, 5, 5]’
- snackbar_animation_dir#
Snackbar animation direction.
Available options are: “Top”, “Bottom”, “Left”, “Right”
snackbar_animation_diris anOptionPropertyand defaults to ‘Bottom’.
- snackbar_x#
The snackbar x position in the screen
snackbar_xis aNumericPropertyand defaults to 0dp.
- snackbar_y#
The snackbar x position in the screen
snackbar_yis aNumericPropertyand defaults to 0dp.
- dismiss(self, *args)#
Dismiss the snackbar.
- open(self)#
Show the snackbar.
- on_open(self, *args)#
Called when a dialog is opened.
- on_dismiss(self, *args)#
Called when the dialog is closed.
- on_buttons(self, instance, value)#
- class kivymd.uix.snackbar.snackbar.Snackbar(**kwargs)#
Snackbar inherits all its functionality from BaseSnackbar
- text#
The text that will appear in the snackbar.
textis aStringPropertyand defaults to ‘’.
- font_size#
The font size of the text that will appear in the snackbar.
font_sizeis aNumericPropertyand defaults to ’15sp’.