Sunday, October 17, 2021

Alert Dialog Boxes

 

Click Alert Popup button

Click Yes, Neat button

alert.py


from kivy.lang import Builder

from kivymd.app import MDApp

from kivymd.uix.dialog import MDDialog

from kivymd.uix.button import MDFlatButton, MDRectangleFlatButton


class MainApp(MDApp):

    dialog = None

    def build(self):

        self.theme_cls.theme_style = "Dark"

        self.theme_cls.primary_palette = "BlueGray"

        return Builder.load_file('alert.kv')


    def show_alert_dialog(self):

        if not self.dialog:

            self.dialog = MDDialog(

                title = "Pretty Neat",

                text = "Some Text",

                buttons =[

                    MDFlatButton(

                        text="Cancel",

                        text_color=self.theme_cls.primary_color,

                        on_release = self.close_dialog

                        ), 

                    MDRectangleFlatButton(

                        text="Yes, Neat",

                        text_color=self.theme_cls.primary_color,

                        on_release = self.neat_dialog

                        ),

                    ],

                )

        self.dialog.open()


    # Click Cancel Button

    def close_dialog(self, obj):

        # Close alert box

        self.dialog.dismiss()


    # Click Neat Button

    def neat_dialog(self, obj):

        # Close alert box

        self.dialog.dismiss()

        # Change label text

        self.root.ids.my_label.text = "Yup"


MainApp().run()


alert.kv

BoxLayout:
    orientation: 'vertical'

    MDScreen:
        MDRectangleFlatButton:
            text: "Alert Popup"
            pos_hint: {'center_x': .5, 'center_y': .5}
            on_release: app.show_alert_dialog()

        MDLabel:
            id: my_label
            text: "Some Stuff"
            pos_hint: {'center_x': .95, 'center_y': .4}