Skip to content
Advertisement

how to insert a MDList in a MDDialog in kivyMD?

I need to show the data from a dictionary in a ThreeLineListItem inside a MDDialog, but my code doesn’t seem to work.

Here’s my code:

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()

Do I need to import the values for the MDList from the KV file?

Advertisement

Answer

You can’t call your MDDialog class like : DialogContent().check_conflicts(product_dict).

You need to reach self.dialog.Because you created this with your custom settings and custom content. Otherwise, you just calling another BoxLayout which name is DialogContent. You can use self.dialog name for call your created dialog.

You need to run your check_conflicts function like that:

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()
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement