Dialog#
#
See also
Dialogs provide important prompts in a user flow.
Use dialogs to make sure users act on information
Two types: basic and full-screen (full-screen not provided in KivyMD)
Should be dedicated to completing a single task
Can also display information relevant to the task
Commonly used to confirm high-risk actions like deleting progress
Anatomy#
Example#
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivymd.app import MDApp
from kivymd.uix.button import MDButton, MDButtonText
from kivymd.uix.dialog import (
MDDialog,
MDDialogIcon,
MDDialogHeadlineText,
MDDialogSupportingText,
MDDialogButtonContainer,
MDDialogContentContainer,
)
from kivymd.uix.divider import MDDivider
from kivymd.uix.list import (
MDListItem,
MDListItemLeadingIcon,
MDListItemSupportingText,
)
KV = '''
MDScreen:
md_bg_color: self.theme_cls.backgroundColor
MDButton:
pos_hint: {'center_x': .5, 'center_y': .5}
on_release: app.show_alert_dialog()
MDButtonText:
text: "Show dialog"
'''
class Example(MDApp):
def build(self):
return Builder.load_string(KV)
def show_alert_dialog(self):
MDDialog(
# ----------------------------Icon-----------------------------
MDDialogIcon(
icon="refresh",
),
# -----------------------Headline text-------------------------
MDDialogHeadlineText(
text="Reset settings?",
),
# -----------------------Supporting text-----------------------
MDDialogSupportingText(
text="This will reset your app preferences back to their "
"default settings. The following accounts will also "
"be signed out:",
),
# -----------------------Custom content------------------------
MDDialogContentContainer(
MDDivider(),
MDListItem(
MDListItemLeadingIcon(
icon="gmail",
),
MDListItemSupportingText(
text="KivyMD-library@yandex.com",
),
theme_bg_color="Custom",
md_bg_color=self.theme_cls.transparentColor,
),
MDListItem(
MDListItemLeadingIcon(
icon="gmail",
),
MDListItemSupportingText(
text="kivydevelopment@gmail.com",
),
theme_bg_color="Custom",
md_bg_color=self.theme_cls.transparentColor,
),
MDDivider(),
orientation="vertical",
),
# ---------------------Button container------------------------
MDDialogButtonContainer(
Widget(),
MDButton(
MDButtonText(text="Cancel"),
style="text",
),
MDButton(
MDButtonText(text="Accept"),
style="text",
),
spacing="8dp",
),
# -------------------------------------------------------------
).open()
Example().run()
from kivymd.app import MDApp
from kivymd.uix.button import MDButton, MDButtonText
from kivymd.uix.dialog import (
MDDialog,
MDDialogIcon,
MDDialogHeadlineText,
MDDialogSupportingText,
MDDialogContentContainer,
MDDialogButtonContainer,
)
from kivymd.uix.divider import MDDivider
from kivymd.uix.list import (
MDListItem, MDListItemSupportingText, MDListItemLeadingIcon
)
from kivymd.uix.screen import MDScreen
from kivymd.uix.widget import MDWidget
class Example(MDApp):
def build(self):
self.theme_cls.primary_palette = "Olive"
return (
MDScreen(
MDButton(
MDButtonText(
text="Show dialog"
),
id="button",
pos_hint={'center_x': .5, 'center_y': 0.5},
on_release=self.show_alert_dialog,
),
md_bg_color=self.theme_cls.backgroundColor
)
)
def show_alert_dialog(self, *args):
MDDialog(
# ----------------------------Icon-----------------------------
MDDialogIcon(
icon="refresh",
),
# -----------------------Headline text-------------------------
MDDialogHeadlineText(
text="Reset settings?",
),
# -----------------------Supporting text-----------------------
MDDialogSupportingText(
text="This will reset your app preferences back to their "
"default settings. The following accounts will also "
"be signed out:",
),
# -----------------------Custom content------------------------
MDDialogContentContainer(
MDDivider(),
MDListItem(
MDListItemLeadingIcon(
icon="gmail",
),
MDListItemSupportingText(
text="KivyMD-library@yandex.com",
),
theme_bg_color="Custom",
md_bg_color=self.theme_cls.transparentColor,
),
MDListItem(
MDListItemLeadingIcon(
icon="gmail",
),
MDListItemSupportingText(
text="kivydevelopment@gmail.com",
),
theme_bg_color="Custom",
md_bg_color=self.theme_cls.transparentColor,
),
MDDivider(),
orientation="vertical",
),
# ---------------------Button container------------------------
MDDialogButtonContainer(
MDWidget(),
MDButton(
MDButtonText(text="Cancel"),
style="text",
),
MDButton(
MDButtonText(text="Accept"),
style="text",
),
spacing="8dp",
),
# -------------------------------------------------------------
).open()
Example().run()
Warning
Do not try to use the MDDialog widget in KV files.
iOS liquid glass dialog#
from kivy.lang import Builder
from kivy.properties import StringProperty, ColorProperty
from kivymd.app import MDApp
from kivymd.uix.dialog import (
IOSDialog,
IOSDialogButtonContainer,
IOSDialogMessage,
IOSDialogTitle,
IOSDialogButton,
)
KV = '''
<CommonIOSDialogButton>
IOSDialogButtonText:
text: root.text
theme_text_color: "Custom"
text_color: root.color
MDScreen:
FitImage:
id: bg_image
source: "https://picsum.photos/800/600?random=2"
IOSButton:
target_background: bg_image
pos_hint: {"center_x": .5, "center_y": .5}
border_radius: [dp(22)] * 4
on_release: app.show_dialog()
IOSButtonText:
text: "Open iOS Dialog"
'''
class CommonIOSDialogButton(IOSDialogButton):
text = StringProperty()
color = ColorProperty("white")
class Example(MDApp):
def build(self):
return Builder.load_string(KV)
def show_dialog(self, *args):
dialog = IOSDialog(target_background=self.root)
dialog.add_widget(IOSDialogTitle(text="Delete application?", bold=True))
dialog.add_widget(
IOSDialogMessage(
text=(
"This will also result in the deletion of all data "
"associated with it."
)
)
)
button_container = IOSDialogButtonContainer(orientation="vertical")
cancel_btn = CommonIOSDialogButton(text="Cancel")
cancel_btn.bind(on_release=lambda x: dialog.dismiss())
delete_btn = CommonIOSDialogButton(text="Delete", color="red")
delete_btn.bind(on_release=lambda x: dialog.dismiss())
button_container.add_widget(cancel_btn)
button_container.add_widget(delete_btn)
dialog.add_widget(button_container)
dialog.open()
if __name__ == "__main__":
Example().run()
from kivy.metrics import dp
from kivy.properties import StringProperty, ColorProperty
from kivymd.app import MDApp
from kivymd.uix.button import IOSButton, IOSButtonText
from kivymd.uix.dialog import (
IOSDialog,
IOSDialogButtonContainer,
IOSDialogMessage,
IOSDialogTitle,
IOSDialogButton,
IOSDialogButtonText,
)
from kivymd.uix.fitimage import FitImage
from kivymd.uix.screen import MDScreen
class CommonIOSDialogButton(IOSDialogButton):
text = StringProperty()
color = ColorProperty("white")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.widgets = [
IOSDialogButtonText(
text=self.text,
theme_text_color="Custom",
text_color=self.color,
)
]
class Example(MDApp):
dialog = IOSDialog
def build(self):
image = FitImage(source="https://picsum.photos/800/600?random=2")
return (
MDScreen(
image,
IOSButton(
IOSButtonText(
text="Open iOS Dialog",
),
target_background=image,
pos_hint={"center_x": .5, "center_y": .5},
border_radius=[dp(22)] * 4,
on_release=lambda x: self.show_dialog(),
)
)
)
def dialog_dismiss(self, *args):
self.dialog.dismiss()
def show_dialog(self, *args):
self.dialog = IOSDialog(
IOSDialogTitle(
text="Delete application?",
bold=True,
),
IOSDialogMessage(
text=(
"This will also result in the deletion of all data "
"associated with it."
)
),
IOSDialogButtonContainer(
CommonIOSDialogButton(
text="Cancel",
on_release=lambda x: self.dialog_dismiss(),
),
CommonIOSDialogButton(
text="Delete",
color="red",
on_release=lambda x: self.dialog_dismiss(),
),
orientation="vertical",
),
target_background=self.root,
)
self.dialog.open()
if __name__ == "__main__":
Example().run()
API break#
1.2.0 version#
from kivy.uix.widget import Widget
from kivymd.app import MDApp
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
class Example(MDApp):
def build(self):
return Widget()
def on_start(self):
MDDialog(
title="Discard draft?",
buttons=[
MDFlatButton(
text="CANCEL",
theme_text_color="Custom",
text_color=self.theme_cls.primary_color,
),
MDFlatButton(
text="DISCARD",
theme_text_color="Custom",
text_color=self.theme_cls.primary_color,
),
],
).open()
Example().run()
from kivy.uix.widget import Widget
from kivymd.app import MDApp
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
class Example(MDApp):
def build(self):
return Widget()
def on_start(self):
MDDialog(
title="Discard draft?",
text="This will reset your device to its default factory settings.",
buttons=[
MDFlatButton(
text="CANCEL",
theme_text_color="Custom",
text_color=self.theme_cls.primary_color,
),
MDFlatButton(
text="DISCARD",
theme_text_color="Custom",
text_color=self.theme_cls.primary_color,
),
],
).open()
Example().run()
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.widget import Widget
from kivymd import images_path
from kivymd.app import MDApp
from kivymd.uix.dialog import MDDialog
from kivymd.uix.list import OneLineAvatarListItem
KV = '''
<Item>
ImageLeftWidget:
source: root.source
'''
class Item(OneLineAvatarListItem):
divider = None
source = StringProperty()
class Example(MDApp):
def build(self):
Builder.load_string(KV)
return Widget()
def on_start(self):
MDDialog(
title="Set backup account",
type="simple",
items=[
Item(text="user01@gmail.com", source=f"{images_path}/logo/kivymd-icon-128.png"),
Item(text="user02@gmail.com", source="data/logo/kivy-icon-128.png"),
],
).open()
Example().run()
2.0.0 version#
from kivy.uix.widget import Widget
from kivymd.uix.widget import MDWidget
from kivymd.app import MDApp
from kivymd.uix.button import MDButton, MDButtonText
from kivymd.uix.dialog import MDDialog, MDDialogHeadlineText, MDDialogButtonContainer
class Example(MDApp):
def build(self):
return MDWidget(md_bg_color=self.theme_cls.backgroundColor)
def on_start(self):
MDDialog(
MDDialogHeadlineText(
text="Discard draft?",
halign="left",
),
MDDialogButtonContainer(
Widget(),
MDButton(
MDButtonText(text="Cancel"),
style="text",
),
MDButton(
MDButtonText(text="Discard"),
style="text",
),
spacing="8dp",
),
).open()
Example().run()
from kivy.uix.widget import Widget
from kivymd.uix.widget import MDWidget
from kivymd.app import MDApp
from kivymd.uix.button import MDButton, MDButtonText
from kivymd.uix.dialog import MDDialog, MDDialogHeadlineText, MDDialogButtonContainer
class Example(MDApp):
def build(self):
return MDWidget(md_bg_color=self.theme_cls.backgroundColor)
def on_start(self):
MDDialog(
MDDialogHeadlineText(
text="Discard draft?",
halign="left",
),
MDDialogSupportingText(
text="This will reset your device to its default factory settings.",
halign="left",
),
MDDialogButtonContainer(
Widget(),
MDButton(
MDButtonText(text="Cancel"),
style="text",
),
MDButton(
MDButtonText(text="Discard"),
style="text",
),
spacing="8dp",
),
).open()
Example().run()
from kivymd import images_path
from kivymd.uix.widget import MDWidget
from kivymd.app import MDApp
from kivymd.uix.dialog import (
MDDialog,
MDDialogHeadlineText,
MDDialogContentContainer,
)
from kivymd.uix.list import (
MDListItem,
MDListItemLeadingAvatar,
MDListItemSupportingText,
)
class Example(MDApp):
def build(self):
return MDWidget(md_bg_color=self.theme_cls.backgroundColor)
def on_start(self):
MDDialog(
MDDialogHeadlineText(
text="Set backup account",
halign="left",
),
MDDialogContentContainer(
MDListItem(
MDListItemLeadingAvatar(
source=f"{images_path}/logo/kivymd-icon-128.png",
),
MDListItemSupportingText(
text="user01@gmail.com",
),
theme_bg_color="Custom",
md_bg_color=self.theme_cls.transparentColor,
),
MDListItem(
MDListItemLeadingAvatar(
source="data/logo/kivy-icon-128.png",
),
MDListItemSupportingText(
text="user01@gmail.com",
),
theme_bg_color="Custom",
md_bg_color=self.theme_cls.transparentColor,
),
orientation="vertical",
),
).open()
Example().run()
API - kivymd.uix.dialog.dialog#
- class kivymd.uix.dialog.dialog.MDDialog(*args, **kwargs)#
Dialog class.
For more information, see in the
MDCardandMotionDialogBehaviorBaseDialogclasses documentation.- Events:
- on_pre_open:
Fired before the MDDialog is opened. When this event is fired MDDialog is not yet added to window.
- on_open:
Fired when the MDDialog is opened.
- on_pre_dismiss:
Fired before the MDDialog is closed.
- on_dismiss:
Fired when the MDDialog is closed. If the callback returns True, the dismiss will be canceled.
- width_offset#
Dialog offset from device width.
width_offsetis anNumericPropertyand defaults to dp(48).
- radius#
Dialog corners rounding value.
radiusis anVariableListPropertyand defaults to [dp(28), dp(28), dp(28), dp(28)].
- scrim_color#
Color for scrim in (r, g, b, a) or string format.
scrim_coloris aColorPropertyand defaults to [0, 0, 0, 0.5].
- auto_dismiss#
This property determines if the dialog is automatically dismissed when the user clicks outside it.
Added in version 2.0.0.
auto_dismissis aBooleanPropertyand defaults to True.
- add_widget(widget, *args, **kwargs)#
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)
- 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.
- class kivymd.uix.dialog.dialog.MDDialogIcon(*args, **kwargs)#
The class implements an icon.
For more information, see in the
MDIconclass documentation.
- class kivymd.uix.dialog.dialog.MDDialogHeadlineText(*args, **kwargs)#
The class implements the headline text.
For more information, see in the
MDLabelclass documentation.
- class kivymd.uix.dialog.dialog.MDDialogSupportingText(*args, **kwargs)#
The class implements the supporting text.
For more information, see in the
MDLabelclass documentation.
- class kivymd.uix.dialog.dialog.MDDialogContentContainer(*args, **kwargs)#
The class implements the container for custom widgets.
For more information, see in the
DeclarativeBehaviorandBoxLayoutclasses documentation.
- class kivymd.uix.dialog.dialog.MDDialogButtonContainer(*args, **kwargs)#
The class implements a container for placing dialog buttons.
For more information, see in the
DeclarativeBehaviorandBoxLayoutclasses documentation.
- class kivymd.uix.dialog.dialog.IOSDialog(*args, **kwargs)#
iOS style dialog class.
Added in version 2.0.1.
For more information, see in the
DeclarativeBehaviorand,IOSGlassBehaviorand,IOSButtonBehaviorand,ScaleBehaviorand,StencilBehaviorand,BaseDialog, andFloatLayoutclasses documentation.- Events:
- on_pre_open:
Fired before the IOSDialog is opened. When this event is fired IOSDialog is not yet added to window.
- on_open:
Fired when the IOSDialog is opened.
- on_pre_dismiss:
Fired before the IOSDialog is closed.
- on_dismiss:
Fired when the IOSDialog is closed. If the callback returns True, the dismiss will be canceled.
- radius#
Dialog corners rounding value.
radiusis aVariableListPropertyand defaults to [dp(18), dp(18), dp(18), dp(18)].
- scrim_color#
Color for scrim in (r, g, b, a) format.
scrim_coloris aColorPropertyand defaults to [0, 0, 0, 0.4].
- auto_dismiss#
This property determines if the dialog is automatically dismissed when the user clicks outside it.
auto_dismissis aBooleanPropertyand defaults to True.
- show_transition#
Animation transition type for dialog open.
show_transitionis aStringPropertyand defaults to ‘easing_standard’.
- show_duration#
Animation duration for dialog open in seconds.
show_durationis aNumericPropertyand defaults to 0.2.
- hide_transition#
Animation transition type for dialog close.
hide_transitionis aStringPropertyand defaults to ‘easing_accelerated’.
- hide_duration#
Animation duration for dialog close in seconds.
hide_durationis aNumericPropertyand defaults to 0.15.
- lens_power#
Magnification power at the center of the lens.
lens_poweris anNumericPropertyand defaults to 0.08.
- bevel_power#
Light refraction power at the bevel/edges.
bevel_poweris anNumericPropertyand defaults to 0.15.
- on_parent(instance, parent)#
Cleanup when removing a widget from the layout.
- add_widget(widget, *args, **kwargs)#
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)
- 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_move(touch)#
Receive a touch move event. The touch is in parent coordinates.
See
on_touch_down()for more information.
- on_touch_up(touch)#
Receive a touch up event. The touch is in parent coordinates.
See
on_touch_down()for more information.
- class kivymd.uix.dialog.dialog.IOSDialogTitle(*args, **kwargs)#
iOS dialog title class.
Added in version 2.0.1.
For more information, see in the
MDLabelclass documentation.
- class kivymd.uix.dialog.dialog.IOSDialogMessage(*args, **kwargs)#
iOS dialog message class.
Added in version 2.0.1.
For more information, see in the
MDLabelclass documentation.
- class kivymd.uix.dialog.dialog.IOSDialogContentContainer(*args, **kwargs)#
iOS custom content container.
Added in version 2.0.1.
For more information, see in the
DeclarativeBehaviorandBoxLayoutclasses documentation.
- class kivymd.uix.dialog.dialog.IOSDialogButtonContainer(*args, **kwargs)#
iOS custom button container class.
Added in version 2.0.1.
For more information, see in the
DeclarativeBehaviorandBoxLayoutclasses documentation.
- class kivymd.uix.dialog.dialog.IOSDialogButton(*args, **kwargs)#
iOS dialog button class.
Added in version 2.0.1.
For more information, see in the
MDButtonclass documentation.
- class kivymd.uix.dialog.dialog.IOSDialogButtonText(*args, **kwargs)#
iOS dialog button text class.
Added in version 2.0.1.
For more information, see in the
MDButtonTextclass documentation.