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):
return Builder.load_string(KV)
def show_alert_dialog(self):
if not self.dialog:
self.dialog = MDDialog(
text="Discard draft?",
buttons=[
MDFlatButton(
text="CANCEL", text_color=self.theme_cls.primary_color
),
MDFlatButton(
text="DISCARD", text_color=self.theme_cls.primary_color
),
],
)
self.dialog.open()
Example().run()
API - kivymd.uix.dialog
¶
- class kivymd.uix.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’.
- title¶
Title dialog.
[...] self.dialog = MDDialog( title="Reset settings?", buttons=[ MDFlatButton( text="CANCEL", text_color=self.theme_cls.primary_color ), MDFlatButton( text="ACCEPT", 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", text_color=self.theme_cls.primary_color ), MDFlatButton( text="ACCEPT", 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): 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="user-1.png"), Item(text="user02@gmail.com", source="user-2.png"), Item(text="Add account", source="add-icon.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): 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", text_color=self.theme_cls.primary_color ), MDFlatButton( text="OK", 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): 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", text_color=self.theme_cls.primary_color ), MDFlatButton( text="OK", 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 format (r, g, b, a).
md_bg_color
is anColorProperty
and defaults to None.
- update_width(self, *args)¶
- update_height(self, *args)¶
- on_open(self)¶
- get_normal_height(self)¶
- edit_padding_for_item(self, instance_item)¶
- create_items(self)¶
- create_buttons(self)¶