如何动态更改 MDDialog KivyMD 中的文本字段?

How to dynamically change a textfield in a MDDialog KivyMD?

我需要在对话框中捕获一些产品信息以提供 MDList,用户必须输入产品代码并且有一个禁用的文本字段,我希望在其中显示产品名称,我想当引入不同的代码时,在其文本字段中动态显示此名称。代码和名称在字典中相关。

我必须从 kv 文件还是从 py 文件来解决这个问题?

这是我需要的类似应用程序:

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 TwoLineListItem
from kivymd.uix.snackbar import Snackbar



product_dict={'1': 'name 1', '2': 'name 2', '3': 'name 3'}
product_dict_values=list(product_dict.values())
product_dict_keys=list(product_dict.keys())

class MainWindow(Screen):
    pass

class SecondWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass

class DialogContent(BoxLayout):
    pass

class MainApp(MDApp):

    dialog=None

    def build(self):
        self.theme_cls.theme_style="Dark"
        self.theme_cls.primary_palette="Green"
        return Builder.load_file('exam.kv')




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

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

        if self.dialog.content_cls.ids.code.text not in product_dict_keys:
            self.dialog.content_cls.ids.code.text=""
            Snackbar(text='Code not in product_dict').open()

        else:
            self.root.get_screen('main').ids.list.add_widget(
                TwoLineListItem(text=f'Name: {self.dialog.content_cls.ids.name.text}',
                    secondary_text=f'Code: {self.dialog.content_cls.ids.code.text}'

                )
            )
            #clear text fields after confirmation
            self.dialog.content_cls.ids.name.text=""
            self.dialog.content_cls.ids.code.text=""

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

    def show_dialog(self):

        if not self.dialog:
            self.dialog=MDDialog(
                title='Add details',
                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

WindowManager:
    MainWindow:
    SecondWindow:

<DialogContent>
    id: dialog
    orientation: "vertical"
    spacing: "10dp"
    size_hint_y: None
    height: "160dp"


    MDTextField:
        id: name
        text: self.text #self.text if code.text is None else product_dict[code.text]
        disabled: True
        hint_text: "Name"
        #required: True


    MDTextField:
        id: code
        hint_text: "Code"
        required: True



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



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


            MDRaisedButton:
                text: 'fill'
                on_release:
                    app.show_dialog()


            AnchorLayout:
                adaptive_height:True
                ScrollView:
                    MDList:
                        id: list

您需要将 DialogContent BoxLayout 从 .kv 文件连接到 .py file.For,我们需要使用 ObjectProperty。现在,我们可以检查写好的代码了。

.py 文件:

from kivy.properties import ObjectProperty()
........
class DialogContent(BoxLayout):
    name = ObjectProperty()
    def texting(self,text):
        if text == '':
            self.name.text=''
        else:
            for key,value in product_dict.items():
                if text == key:
                    self.name.text = product_dict[text]
                    break
            else:
                self.name.text = '(Code not Valid)'
........

.kv 文件:

........
<DialogContent>
    name:name
    id: dialog
    orientation: "vertical"
    spacing: "10dp"
    size_hint_y: None
    height: "160dp"
    
    MDTextField:
        id: name
        text: self.text #self.text if code.text is None else product_dict[code.text]
        disabled: True
        hint_text: "Name"
        #required: True

    MDTextField:
        id: code
        hint_text: "Code"
        required: True
        on_text: root.texting(self.text)
........