I'm trying to add a changeable list in a custom dialog in kivyMD. the problem is when I call self.adding()
, the list appears in my FloatLayout
, not in the dialog.
I know that's because I called "Content" in my FloatLayout
. I did this because I wanted to use ObjectProperty but it didn't work well. I really don't know what can I do to solve this problem.
Sorry for my broken English.
code:
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
from kivymd.uix.list import OneLineListItem,MDList
from kivy.properties import ObjectProperty
KV = '''
<Content>
name:"content"
draw:container
orientation: "vertical"
ScrollView:
MDList:
id: container
FloatLayout:
mgr:cont
Content:
id:cont
MDFlatButton:
text: "ALERT DIALOG"
pos_hint: {'center_x': .5, 'center_y': .5}
on_release: app.show_confirmation_dialog()
'''
class Content(BoxLayout):
draw=ObjectProperty(None)
class Example(MDApp):
dialog = None
mgr=ObjectProperty(None)
def adding(self):
for i in range(20):
self.root.mgr.draw.add_widget(OneLineListItem(text=f"Single-line item {i}"))
def build(self):
return Builder.load_string(KV)
def show_confirmation_dialog(self):
# def adding(self):
# for i in range(20):
# container.add_widget(OneLineListItem(text=f"Single-line item {i}"))
if not self.dialog:
self.dialog = MDDialog(
title="Address:",
type="custom",
content_cls=Content(),
)
self.adding()
self.dialog.open()
Example().run()
I fixed the problem by editing my code to this :
NEW CODE:
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
from kivymd.uix.list import OneLineListItem,MDList
KV = '''
<Content>
# name:"content"
# draw:container
orientation: "vertical"
ScrollView:
MDList:
id: container
FloatLayout:
# mgr:cont
# Content:
# id:cont
MDFlatButton:
text: "ALERT DIALOG"
pos_hint: {'center_x': .5, 'center_y': .5}
on_release: app.show_confirmation_dialog()
'''
class Content(BoxLayout):
def __init__(self, *args, **kwargs):
super().__init__(**kwargs)
self.container= self.ids.container
print("content called")
def adding(self):
for i in range(20):
self.container.add_widget(OneLineListItem(text=f"Single-line item {i}"))
print("adding called")
adding(self)
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="Address:",
type="custom",
content_cls=Content(),
)
self.dialog.open()
Example().run()