Python3 KivyMD - MDDialog 获取 ItemConfirm 文本值

Python3 KivyMD - MDDialog get ItemConfirm text value

我想在按下 OK 按钮后将我的 MMDialog 中的选定项目传递给我的 project_selected 函数。但是我不知道如何获取值并执行此操作。

我可以在 ItemConfirm class 中打印 set_icon 函数中的值,但我不知道传递该值的更好方法是什么到 HomeWindow(Screen) class 或者一旦 ItemConfirm 已经从作为 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()

您可以按如下方式实现,

  1. 首先创建一个道具。比如,selected_projectHomeWindow 中,
class HomeWindow(Screen):
    dialog = None
    selected_project = StringProperty()
  1. 现在在方法 set_icon 中将其值设置为,
    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. 现在是时候在方法 project_selected,
  2. 中设置这个值了
    def project_selected(self, *args, **kwargs):
        self.ids.project_selection.text = self.selected_project