You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

KivyMD自定义对话框用户输入存储位置及获取方法咨询

如何获取KivyMD自定义对话框中的用户输入并打印

要获取对话框里用户输入的城市名称并打印到控制台,你需要给输入框添加可识别的标识,同时给OK按钮绑定点击事件来触发获取逻辑,具体修改如下:

1. 给输入框添加ID

在KV语言的<Content>块里,给两个输入框分别加上id属性,这样后续可以通过ID定位到输入框实例:

<Content>
    orientation: "vertical"
    spacing: "12dp"
    size_hint_y: None
    height: "120dp"

    MDTextField:
        id: city_input  # 添加城市输入框ID
        hint_text: "City"

    MDTextField:
        id: street_input  # 添加街道输入框ID
        hint_text: "Street"

2. 给OK按钮绑定事件

在创建MDDialog的按钮列表中,给OK按钮添加on_release事件,调用自定义的方法来获取输入:

MDFlatButton(
    text="OK",
    theme_text_color="Custom",
    text_color=self.theme_cls.primary_color,
    on_release=self.get_user_input,  # 绑定获取输入的方法
),

3. 实现获取输入的方法

在Example类中添加get_user_input方法,通过对话框的content_cls属性访问输入框的text值,完成打印后关闭对话框:

def get_user_input(self, instance):
    # 通过ID获取城市输入框的文本内容
    city_name = self.dialog.content_cls.ids.city_input.text
    print(f"输入的城市名称:{city_name}")
    # 可选:获取街道输入
    street_name = self.dialog.content_cls.ids.street_input.text
    print(f"输入的街道名称:{street_name}")
    # 关闭对话框
    self.dialog.dismiss()

修改后的完整代码

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

KV = '''
<Content>
    orientation: "vertical"
    spacing: "12dp"
    size_hint_y: None
    height: "120dp"

    MDTextField:
        id: city_input
        hint_text: "City"

    MDTextField:
        id: street_input
        hint_text: "Street"


MDFloatLayout:

    MDFlatButton:
        text: "ALERT DIALOG"
        pos_hint: {'center_x': .5, 'center_y': .5}
        on_release: app.show_confirmation_dialog()
'''


class Content(BoxLayout):
    pass


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(),
                buttons=[
                    MDFlatButton(
                        text="CANCEL",
                        theme_text_color="Custom",
                        text_color=self.theme_cls.primary_color,
                        on_release=lambda x: self.dialog.dismiss()  # 给取消按钮也加上关闭逻辑
                    ),
                    MDFlatButton(
                        text="OK",
                        theme_text_color="Custom",
                        text_color=self.theme_cls.primary_color,
                        on_release=self.get_user_input,
                    ),
                ],
            )
        self.dialog.open()

    def get_user_input(self, instance):
        city_name = self.dialog.content_cls.ids.city_input.text
        print(f"输入的城市名称:{city_name}")
        street_name = self.dialog.content_cls.ids.street_input.text
        print(f"输入的街道名称:{street_name}")
        self.dialog.dismiss()


Example().run()

原理说明

  • 给输入框加id是为了在Python代码中精准定位到对应的输入控件;
  • 按钮绑定on_release事件后,点击时会触发指定方法,在方法里通过content_cls.ids访问输入框的text属性,就能拿到用户输入的内容;
  • 最后调用dialog.dismiss()关闭对话框,提升交互体验。

内容的提问来源于stack exchange,提问作者user140566

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 21:11:27