Dialog#
See also Dialogs inform users about a task and can contain critical
information, require decisions, or involve multiple tasks.
Usage#
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
KV = '''
MDFloatLayout:
MDFlatButton:
text: "ALERT DIALOG"
pos_hint: {'center_x': .5, 'center_y': .5}
on_release: app.show_alert_dialog()
'''
class Example(MDApp):
dialog = None
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
return Builder.load_string(KV)
def show_alert_dialog(self):
if not self.dialog:
self.dialog = MDDialog(
text="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,
),
],
)
self.dialog.open()
Example().run()
API - kivymd.uix.dialog.dialog
#
- class kivymd.uix.dialog.dialog.BaseDialog(**kwargs)#
ModalView class. See module documentation for more information.
- Events:
- on_pre_open:
Fired before the ModalView is opened. When this event is fired ModalView is not yet added to window.
- on_open:
Fired when the ModalView is opened.
- on_pre_dismiss:
Fired before the ModalView is closed.
- on_dismiss:
Fired when the ModalView is closed. If the callback returns True, the dismiss will be canceled.
Changed in version 1.11.0: Added events on_pre_open and on_pre_dismiss.
Changed in version 2.0.0: Added property ‘overlay_color’.
Changed in version 2.1.0: Marked attach_to property as deprecated.
- elevation#
See
kivymd.uix.behaviors.elevation.CommonElevationBehavior.elevation
attribute for more information.New in version 1.1.0.
elevation
is anNumericProperty
and defaults to 3.
- shadow_softness#
See
kivymd.uix.behaviors.elevation.CommonElevationBehavior.shadow_softness
attribute for more information.New in version 1.1.0.
shadow_softness
is anNumericProperty
and defaults to 24.
- shadow_offset#
See
kivymd.uix.behaviors.elevation.CommonElevationBehavior.shadow_offset
attribute for more information.New in version 1.1.0.
shadow_offset
is anListProperty
and defaults to [0, 4].
- radius#
Dialog corners rounding value.
[...] self.dialog = MDDialog( text="Oops! Something seems to have gone wrong!", radius=[20, 7, 20, 7], ) [...]
radius
is anListProperty
and defaults to [7, 7, 7, 7].
- class kivymd.uix.dialog.dialog.MDDialog(**kwargs)#
ModalView class. See module documentation for more information.
- Events:
- on_pre_open:
Fired before the ModalView is opened. When this event is fired ModalView is not yet added to window.
- on_open:
Fired when the ModalView is opened.
- on_pre_dismiss:
Fired before the ModalView is closed.
- on_dismiss:
Fired when the ModalView is closed. If the callback returns True, the dismiss will be canceled.
Changed in version 1.11.0: Added events on_pre_open and on_pre_dismiss.
Changed in version 2.0.0: Added property ‘overlay_color’.
Changed in version 2.1.0: Marked attach_to property as deprecated.
- title#
Title dialog.
[...] self.dialog = MDDialog( title="Reset settings?", buttons=[ MDFlatButton( text="CANCEL", theme_text_color="Custom", text_color=self.theme_cls.primary_color, ), MDFlatButton( text="ACCEPT", theme_text_color="Custom", text_color=self.theme_cls.primary_color, ), ], ) [...]
title
is anStringProperty
and defaults to ‘’.
- text#
Text dialog.
[...] self.dialog = MDDialog( title="Reset settings?", 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="ACCEPT", theme_text_color="Custom", text_color=self.theme_cls.primary_color, ), ], ) [...]
text
is anStringProperty
and defaults to ‘’.
- buttons#
List of button objects for dialog. Objects must be inherited from
BaseButton
class.[...] self.dialog = MDDialog( text="Discard draft?", buttons=[ MDFlatButton(text="CANCEL"), MDRaisedButton(text="DISCARD"), ], ) [...]
buttons
is anListProperty
and defaults to [].
- items#
List of items objects for dialog. Objects must be inherited from
BaseListItem
class.from kivy.lang import Builder from kivy.properties import StringProperty from kivymd.app import MDApp from kivymd.uix.dialog import MDDialog from kivymd.uix.list import OneLineAvatarListItem KV = ''' <Item> ImageLeftWidget: source: root.source MDFloatLayout: MDFlatButton: text: "ALERT DIALOG" pos_hint: {'center_x': .5, 'center_y': .5} on_release: app.show_simple_dialog() ''' class Item(OneLineAvatarListItem): divider = None source = StringProperty() class Example(MDApp): dialog = None def build(self): self.theme_cls.theme_style = "Dark" self.theme_cls.primary_palette = "Orange" return Builder.load_string(KV) def show_simple_dialog(self): if not self.dialog: self.dialog = MDDialog( title="Set backup account", type="simple", items=[ Item(text="user01@gmail.com", source="kivymd/images/logo/kivymd-icon-128.png"), Item(text="user02@gmail.com", source="data/logo/kivy-icon-128.png"), ], ) self.dialog.open() Example().run()
from kivy.lang import Builder from kivymd.app import MDApp from kivymd.uix.button import MDFlatButton from kivymd.uix.dialog import MDDialog from kivymd.uix.list import OneLineAvatarIconListItem KV = ''' <ItemConfirm> on_release: root.set_icon(check) CheckboxLeftWidget: id: check group: "check" MDFloatLayout: MDFlatButton: text: "ALERT DIALOG" pos_hint: {'center_x': .5, 'center_y': .5} on_release: app.show_confirmation_dialog() ''' class ItemConfirm(OneLineAvatarIconListItem): divider = None def set_icon(self, instance_check): instance_check.active = True check_list = instance_check.get_widgets(instance_check.group) for check in check_list: if check != instance_check: check.active = False class Example(MDApp): dialog = None def build(self): self.theme_cls.theme_style = "Dark" self.theme_cls.primary_palette = "Orange" return Builder.load_string(KV) def show_confirmation_dialog(self): if not self.dialog: self.dialog = MDDialog( title="Phone ringtone", type="confirmation", items=[ ItemConfirm(text="Callisto"), ItemConfirm(text="Luna"), ItemConfirm(text="Night"), ItemConfirm(text="Solo"), ItemConfirm(text="Phobos"), ItemConfirm(text="Diamond"), ItemConfirm(text="Sirena"), ItemConfirm(text="Red music"), ItemConfirm(text="Allergio"), ItemConfirm(text="Magic"), ItemConfirm(text="Tic-tac"), ], buttons=[ MDFlatButton( text="CANCEL", theme_text_color="Custom", text_color=self.theme_cls.primary_color, ), MDFlatButton( text="OK", theme_text_color="Custom", text_color=self.theme_cls.primary_color, ), ], ) self.dialog.open() Example().run()
items
is anListProperty
and defaults to [].
- width_offset#
Dialog offset from device width.
width_offset
is anNumericProperty
and defaults to dp(48).
- type#
Dialog type. Available option are ‘alert’, ‘simple’, ‘confirmation’, ‘custom’.
type
is anOptionProperty
and defaults to ‘alert’.
- content_cls#
Custom content class.
from kivy.lang import Builder from kivy.uix.boxlayout import BoxLayout from kivymd.app import MDApp from kivymd.uix.button import MDFlatButton from kivymd.uix.dialog import MDDialog KV = ''' <Content> orientation: "vertical" spacing: "12dp" size_hint_y: None height: "120dp" MDTextField: hint_text: "City" MDTextField: hint_text: "Street" MDFloatLayout: MDFlatButton: text: "ALERT DIALOG" pos_hint: {'center_x': .5, 'center_y': .5} on_release: app.show_confirmation_dialog() ''' class Content(BoxLayout): pass class Example(MDApp): dialog = None def build(self): self.theme_cls.theme_style = "Dark" self.theme_cls.primary_palette = "Orange" return Builder.load_string(KV) def show_confirmation_dialog(self): if not self.dialog: self.dialog = MDDialog( title="Address:", type="custom", content_cls=Content(), buttons=[ MDFlatButton( text="CANCEL", theme_text_color="Custom", text_color=self.theme_cls.primary_color, ), MDFlatButton( text="OK", theme_text_color="Custom", text_color=self.theme_cls.primary_color, ), ], ) self.dialog.open() Example().run()
from kivymd.app import MDApp from kivymd.uix.boxlayout import MDBoxLayout from kivymd.uix.button import MDFlatButton from kivymd.uix.dialog import MDDialog from kivymd.uix.floatlayout import MDFloatLayout from kivymd.uix.textfield import MDTextField class Example(MDApp): dialog = None def build(self): self.theme_cls.theme_style = "Dark" self.theme_cls.primary_palette = "Orange" return ( MDFloatLayout( MDFlatButton( text="ALERT DIALOG", pos_hint={'center_x': 0.5, 'center_y': 0.5}, on_release=self.show_confirmation_dialog, ) ) ) def show_confirmation_dialog(self, *args): if not self.dialog: self.dialog = MDDialog( title="Address:", type="custom", content_cls=MDBoxLayout( MDTextField( hint_text="City", ), MDTextField( hint_text="Street", ), orientation="vertical", spacing="12dp", size_hint_y=None, height="120dp", ), buttons=[ MDFlatButton( text="CANCEL", theme_text_color="Custom", text_color=self.theme_cls.primary_color, ), MDFlatButton( text="OK", theme_text_color="Custom", text_color=self.theme_cls.primary_color, ), ], ) self.dialog.open() Example().run()
content_cls
is anObjectProperty
and defaults to ‘None’.
- md_bg_color#
Background color in the (r, g, b, a) or string format.
md_bg_color
is anColorProperty
and defaults to None.
- update_width(self, *args)#
- update_height(self, *args)#
- on_open(self)#
default open event handler.
- get_normal_height(self)#
- edit_padding_for_item(self, instance_item)#
- create_items(self)#
- create_buttons(self)#