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!").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()
Usage with button¶
snackbar = Snackbar(
text="This is a snackbar!",
snackbar_x="10dp",
snackbar_y="10dp",
)
snackbar.size_hint_x = (
Window.width - (snackbar.snackbar_x * 2)
) / Window.width
snackbar.buttons = [
MDFlatButton(
text="UPDATE",
text_color=(1, 1, 1, 1),
on_release=snackbar.dismiss,
),
MDFlatButton(
text="CANCEL",
text_color=(1, 1, 1, 1),
on_release=snackbar.dismiss,
),
]
snackbar.open()
Using a button with custom color¶
Snackbar(
...
bg_color=(0, 0, 1, 1),
).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 = '''
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 + 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: get_color_from_hex('ffffff')
shorten: True
shorten_from: 'right'
pos_hint: {'center_y': .5}
Screen:
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
¶
- class kivymd.uix.snackbar.Snackbar(**kwargs)¶
Snackbar inherits all its functionality from BaseSnackbar
- 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’.