KivyMD Dialog Box

561 views
Skip to first unread message

Katie Cook

unread,
Jan 10, 2022, 6:55:15 PM1/10/22
to Kivy users support
Hi, 

I've created a dialog pop up box with an item confirmation checkbox list. Does anyone know how to print the text that's assigned to a checkbox that has been check? 

Any help would be really appreciated. 

Many thanks, 

Katie 

Elliot Garbus

unread,
Jan 10, 2022, 8:18:20 PM1/10/22
to kivy-...@googlegroups.com

There are a number of different ways to do this, some of it depends on how you have structured your code, and what you mean by print.  If you just want to print the code the console, you could use on_active event.  I put a small example below.  IF you need more assistance please share a minimal, executable code.

 

from kivymd.app import MDApp
from kivy.lang import Builder


kv =
"""
MDBoxLayout:
    MDCheckbox:
        id: cb
        size_hint_x: None
        width: 48
        on_active: print(f'Label Text: {label.text}; Checkbox is {self.active}')
    MDLabel:
        id: label
        text: 'The text'

"""


class TestCheckBox(MDApp):
   
def build(self):
       
return Builder.load_string(kv)


TestCheckBox().run()

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/bb3ee38e-dbb1-4268-9238-d8a73094b00dn%40googlegroups.com.

 

Katie Cook

unread,
Jan 11, 2022, 6:45:41 AM1/11/22
to Kivy users support
Hi Elliot, 

Thank you for your help. Please may I have more assistance, still a little stuck. Here's an example of some working code. I want the timeslot the user selects to print to the console. Thank you. 

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="Available Appointments",
                type="confirmation",
                items=[
                    ItemConfirm(text="09:00"),
                    ItemConfirm(text="09:30"),
                    ItemConfirm(text="10:00"),
                    ItemConfirm(text="10:30"),
                    ItemConfirm(text="11:00"),
                    ItemConfirm(text="11:30"),
                    ItemConfirm(text="12:00"),
                    ItemConfirm(text="12:30"),
                    ItemConfirm(text="13:00"),
                    ItemConfirm(text="13:30"),
                    ItemConfirm(text="14:00"),
                ],
                buttons=[
                    MDFlatButton(
                        text="CANCEL",
                        theme_text_color="Custom",
                        text_color=self.theme_cls.primary_color,
                        on_release=self.close_dialog,
                    ),
                    MDFlatButton(
                        text="OK",
                        theme_text_color="Custom",
                        text_color=self.theme_cls.primary_color,
                        on_release=self.open_dialog,
                    ),
                ],
            )
        self.dialog.open()

    def close_dialog(self,obj):
        self.dialog.dismiss()


    def open_dialog(self, obj):
        self.dialog.dismiss()


Example().run()

Elliot Garbus

unread,
Jan 11, 2022, 7:26:29 AM1/11/22
to kivy-...@googlegroups.com

Code below.  The key change is to the definition of <ItemConfirm> in kv. 

The event on_active fires when the value of active changes.  If active is true, meaning the box is checked then the text value associated with ItemConfrm (root.text) is printed.

The on_release event will fire if the line is clicked and released outside of the checkbox.  We use it to change the state of the checkbox by setting or clearing the active attribute.

 

The use of root and self in kv is described here: https://kivy.org/doc/master/api-kivy.lang.html?highlight=lang#value-expressions-on-property-expressions-ids-and-reserved-keywords

 

 

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: check.active = not check.active

   
    CheckboxLeftWidget:
        id: check
        group: "check"
        on_active: if self.active: print(root.text)



MDFloatLayout:

    MDFlatButton:
        text: "ALERT DIALOG"
        pos_hint: {'center_x': .5, 'center_y': .5}
        on_release: app.show_confirmation_dialog()
'''


class ItemConfirm(OneLineAvatarIconListItem):
   
pass
   
# 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


Reply all
Reply to author
Forward
0 new messages