kivykivy-languagekivymdpython-3.9mddialog

Python3 KivyMD - MDDialog get ItemConfirm text value


I want to pass to my project_selected function the selected item in my MDDialog after pressing the OK button. But I can't figure out how can I get the value and do this.

I could print the value inside my set_icon funtion in ItemConfirm class, but I don't know whats the better way to pass that value to HomeWindow(Screen) class or if it is possible to call it directly from inside of it once that the ItemConfirm is already called from inside a function that is part of HomeWindow class.

main.py

from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.app import MDApp
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
from kivymd.uix.list import OneLineAvatarIconListItem


class ItemConfirm(OneLineAvatarIconListItem):
    divider = None

    def set_icon(self, instance_check):
        print(self.text)
        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 HomeWindow(Screen):
    dialog = None

    def show_confirmation_dialog(self):
        projects = [{id: 0, name: "example1"},{id: 1, name: "example2"}]

        if not self.dialog:
            self.dialog = MDDialog(
                title="Projects",
                type="confirmation",
                auto_dismiss=False,
                items=[ItemConfirm(text=f"{project['name']}") for project in projects],
                buttons=[
                    MDFlatButton(text="CANCEL", theme_text_color="Custom"),
                    MDFlatButton(
                        text="OK",
                        theme_text_color="Custom",
                        on_release=self.project_selected,
                    ),
                ],
            )
        self.dialog.open()

    def project_selected(self, *args, **kwargs):
        self.ids.project_selection.text = self.dialog.text


class WindowManager(ScreenManager):
    pass


class RlogTimer(MDApp):
    def build(self):

        self.theme_cls.theme_style = "Dark"
        self.theme_cls.primary_palette = "BlueGray"

        self.root = Builder.load_file("templates/main.kv")

        return self.root


if __name__ == "__main__":
    RlogTimer().run()

templates/main.kv

#: import NoTransition kivy.uix.screenmanager.NoTransition

WindowManager:
    HomeWindow:
    
<ScreenManager>:
    transition: NoTransition()

<ItemConfirm>:
    on_release: root.set_icon(check)

    CheckboxLeftWidget:
        id: check
        group: "check"

<HomeWindow>:
    name: "home"


    MDBoxLayout:
        orientation: "vertical"

        MDToolbar:
            id: title
            title: "Redmine logTimer"
            right_action_items: [["clock", lambda x: app.callback_2()]]

        MDFlatButton:
            id: project_selection
            text: "Select Project"
            pos_hint: {'center_x': .5, 'center_y': .5}
            on_release: root.show_confirmation_dialog()

Solution

  • You can achieve that as follows,

    1. First create a prop. say, selected_project in HomeWindow as,
    class HomeWindow(Screen):
        dialog = None
        selected_project = StringProperty()
    
    1. Now set its value in method set_icon as,
        def set_icon(self, instance_check):
            ...
            app = MDApp.get_running_app() # Access the running app instance.
            home_screen = app.root.get_screen("home") # Access required screen.
            home_screen.selected_project = self.text # Now set value.
            ...
    
    1. Now it's time to set this value in method project_selected,
        def project_selected(self, *args, **kwargs):
            self.ids.project_selection.text = self.selected_project