如何在 kivyMD 的 MDDialog 中插入一个 MDList?

how to insert a MDList in a MDDialog in kivyMD?

我需要在 MDDialog 中的 ThreeLineListItem 中显示字典中的数据,但我的代码似乎不起作用。

这是我的代码:

from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDRaisedButton, MDFlatButton
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.list import ThreeLineListItem
from kivymd.uix.snackbar import Snackbar
from kivy.properties import ObjectProperty

KV='''
WindowManager:
    #LoginWindow:
    MainWindow:
    SecondWindow:

<DialogContent>
    confirm_check_in_list: confirm_check_in_list
    id: confirm_check_in_dialog
    orientation: "vertical"
    spacing: dp(20)
    size_hint_y: None
    size_hint_x: None
    height: self.height
    width: self.width

    AnchorLayout:
        adaptive_height: True
        ScrollView:

            MDList:
                id: confirm_check_in_list



<MainWindow>
    name: 'main'
    MDBoxLayout:
        orientation: 'vertical'
        MDToolbar:
            title: 'test'



        MDBoxLayout:
            orientation:'vertical'
            spacing: dp(10)
            padding: dp(20)


            MDRaisedButton:
                text: 'Click me'
                on_release:
                    app.show_dialog()

'''

product_dict={'name 1': (1, 2), 'name 2': (3,4), 'name 3':(4,2)}


class MainWindow(Screen):
    pass

class SecondWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass

class DialogContent(BoxLayout):
    confirm_check_in_list=ObjectProperty()
    def check_conflicts(self, conflicts):
        for name, quantity in conflicts.items():
            self.ids.confirm_check_in_list.add_widget(
                ThreeLineListItem(text=f'{name}',
                    secondary_text=f'q1: {quantity[0]}',
                    tertiary_text=f'q2: {quantity[1]}',
                )
            )


class MainApp(MDApp):

    dialog=None

    def build(self):
        self.theme_cls.theme_style="Dark"
        self.theme_cls.primary_palette="Green"
        return Builder.load_string(KV)

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

    def confirm_selection(self, obj):
        #check number in quantity field

        Snackbar(text='Success').open()

    def show_dialog(self):
        DialogContent().check_conflicts(product_dict)

        if not self.dialog:
            self.dialog=MDDialog(
                title='Check products',
                type='custom',
                content_cls=DialogContent(),
                buttons=[
                    MDFlatButton(
                        text='CANCEL',
                        theme_text_color="Custom",
                        text_color=self.theme_cls.primary_color,
                        on_release=self.close_dialog
                    ),
                    MDRaisedButton(
                        text='OK',
                        theme_text_color="Custom",
                        on_release=
                            self.confirm_selection

                    )
                ],
            )
        self.dialog.open()


MainApp().run()

我是否需要从 KV 文件中导入 MDList 的值?

您不能像这样调用 MDDialog class:DialogContent().check_conflicts(product_dict).

您需要达到 self.dialog。因为这是您使用自定义设置和自定义内容创建的。否则,您只是调用另一个名为 DialogContent 的 BoxLayout。您可以使用 self.dialog 名称来调用您创建的对话框。

您需要 运行 您的 check_conflicts 函数:

def show_dialog(self):
    if not self.dialog:
        self.dialog=MDDialog(
            title='Check products',
            type='custom',
            content_cls=DialogContent(),
            buttons=[
                MDFlatButton(
                    text='CANCEL',
                    theme_text_color="Custom",
                    text_color=self.theme_cls.primary_color,
                    on_release=self.close_dialog
                ),
                MDRaisedButton(
                    text='OK',
                    theme_text_color="Custom",
                    on_release=
                        self.confirm_selection

                )
            ],
        )
    self.dialog.content_cls.check_conflicts(product_dict)
    self.dialog.open()