KivyMD如何从动态文本字段获取值?



上次我问了一个关于如何在按钮按下添加多个文本字段的问题:如何在kivymd中添加多个文本字段。我已经做得很好了,但现在我被另一个问题卡住了。我现在的问题是如何获得每个添加的文本字段的值,现在他们没有任何分配的id。我想到了一个解决这个问题的方法,我使用对话框来引用文本,然后将其附加到字典中,但我意识到,如果我在文本字段上编辑它,我将无法获得更改的值。所以现在我真的需要直接引用添加的文本字段上的值。这是我的档案。非常感谢!

from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen
from kivymd.uix.textfield import MDTextField
from kivymd.uix.dialog import MDDialog
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.button import MDFlatButton
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.tab import MDTabsBase

KV = """
ScreenManager:
FormScreen:
<FormScreen>
name: 'form'
MDScreen:
md_bg_color:app.dark2
MDBoxLayout:
orientation: "vertical"
MDToolbar:
md_bg_color:app.dark2
title: "Upload Data"
type_height: "small"
left_action_items: [["arrow-left", lambda x : app.swtchScreen('collections')]]
right_action_items: [["eraser", lambda x : app.add_dict()],["plus", lambda x : app.form_dialog()]]
MDTabs:
id: tabs
background_color: rgba(0,0,0,0)
tab_hint_x: True
Tab:
title: "Passport Data"
MDBoxLayout: # Add main container.
orientation: "vertical"
padding: dp(10)
spacing: dp(5)
MDTextField:
id: input_1
hint_text: "Name"
#                        pos_hint: {"center_x": 0.5, "center_y": 0.95}
#                        size_hint: .75,0.09 # "size_hint_y" will be set automatically.
pos_hint: {"center_x": 0.5}
size_hint_x: .75
color_mode: 'accent'
mode: "rectangle"
#additional textfields
ScrollView:
MDBoxLayout: # Add all text fields in this container.
id: box
orientation: "vertical"
adaptive_height: True # Grow vertically.
<Content>
orientation: "vertical"
spacing: "12dp"
size_hint_y: None
height: "120dp"
MDTextField:
id:title
hint_text: "Title"
MDTextField:
id:desc
hint_text: "Description"
"""

class FormScreen(Screen):
pass
class Content(BoxLayout):
pass
class Tab(MDFloatLayout, MDTabsBase):
pass
class DemoApp(MDApp):
dialog = None
dark2 = 46/255, 139/255, 87/255, 1
dict = {}
def form_dialog(self):
if not self.dialog:
self.dialog = MDDialog(
title="Add Data",
type="custom",
content_cls=Content(),
buttons=[
MDFlatButton(
text="CANCEL",
theme_text_color="Custom",
text_color=self.theme_cls.primary_color,
on_press = lambda x : self.dialog.dismiss(force=True)
),
MDFlatButton(
text="OK",
theme_text_color="Custom",
text_color=self.theme_cls.primary_color,
on_press = lambda x : self.add_textfields()
),
],
)
self.dialog.open()
def add_textfields(self):
self.dialog.dismiss(force=True)
# self.dialog.content_cls.ids.title.text, self.dialog.content_cls.ids.desc.text = "",""
title = self.dialog.content_cls.ids.title.text
desc = self.dialog.content_cls.ids.desc.text
self.dict[f'{title}'] = f'{desc}'
print(self.dict)
self.help.get_screen('form').ids.box.add_widget(
MDTextField(hint_text= title,
text = desc,
size_hint = (.75,0.08),
mode = "rectangle",
color_mode = 'accent',
pos_hint = {"center_x": 0.5}
))
def build(self):
# screen =Screen()

self.title='Demeter'
self.theme_cls.theme_style = "Light"
self.theme_cls.primary_palette = "LightGreen"   
self.help = Builder.load_string(KV)
# screen.add_widget(self.help)
return self.help
DemoApp().run()

您可以通过几种方式实现这一点,其中一种方式是,在将文本字段添加到父字段时保留文本字段的引用(可能在列表/字典中)。另一种方法可能是为该实例创建动态属性,以便稍后通过事件(prop)访问它。回调或事件回调)。

下面是后者的实现:

class DemoApp(MDApp):
dialog = None
dark2 = 46/255, 139/255, 87/255, 1
dict = {} # Using of default keyword is not recommended. May be 'dict_'.
# Add a counter.
count = 0
.
.
.

def add_textfields(self):
self.dialog.dismiss(force=True)
# self.dialog.content_cls.ids.title.text, self.dialog.content_cls.ids.desc.text = "",""
title = self.dialog.content_cls.ids.title.text
desc = self.dialog.content_cls.ids.desc.text
self.dict[f'{title}'] = f'{desc}'
print(self.dict)
# Make sure to change "count" in order to create
# unique dynamic attribute each time.
self.count += 1
# First create an instance.
text_field = MDTextField(hint_text= title,
text = desc,
size_hint = (.75,0.08),
mode = "rectangle",
color_mode = 'accent',
pos_hint = {"center_x": 0.5}
)
# Add a (valid) dynamic attribute of your choice.
text_field.tid = self.count
# I bound it to the event "on_text_validate" by an event callback,
# say, "do_something" as a demonstration.
# Use it if you need to.
text_field.bind(on_text_validate = self.do_something)
# Now add that instance.
self.help.get_screen('form').ids.box.add_widget(text_field)

def do_something(self, t_field):
"""This is the (prop. or event) callback."""
print(f"This is the text from text field with tid  {t_field.tid} :", t_field.text)
.
.
.

相关内容

  • 没有找到相关文章

最新更新