Kivymd 自定义列表对话框。无法在 mddialog 中插入可变列表
Kivymd Custom List Dialog. can't insert changeable list in mddialog
我正在尝试在 kivyMD 的自定义对话框中添加一个可更改的列表。问题是当我调用 self.adding()
时,列表出现在我的 FloatLayout
中,而不是对话框中。
我知道那是因为我在 FloatLayout
中调用了“内容”。我这样做是因为我想使用 ObjectProperty 但效果不佳。我真的不知道我能做些什么来解决这个问题。
抱歉我的英语不好。
代码:
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()
我通过将我的代码编辑为这个解决了这个问题:
新代码:
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()
我正在尝试在 kivyMD 的自定义对话框中添加一个可更改的列表。问题是当我调用 self.adding()
时,列表出现在我的 FloatLayout
中,而不是对话框中。
我知道那是因为我在 FloatLayout
中调用了“内容”。我这样做是因为我想使用 ObjectProperty 但效果不佳。我真的不知道我能做些什么来解决这个问题。
抱歉我的英语不好。
代码:
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()
我通过将我的代码编辑为这个解决了这个问题:
新代码:
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()