【问题标题】:Kivymd Custom Input Dialog. problem with getting textKivymd 自定义输入对话框。获取文本的问题
【发布时间】:2022-05-04 23:18:35
【问题描述】:

我正在使用 kivymd 创建一个输入对话框。每当我尝试从文本字段中获取文本时,它都不会输出文本,而是看起来文本不存在。 (对话框刚刚弹出,按钮工作正常)。

部分kivy代码

<Content>
    MDTextField:
        id: pin
        pos_hint: {"center_x": 0.5, "center_y": 0.5}
        color_mode: 'custom'
        line_color_focus: [0,0,1,1]

部分python代码

class Content(FloatLayout):
    pass

class MenuScreen(Screen):
    def __init__(self, **kwargs):
        super(MenuScreen, self).__init__(**kwargs)

    def show_confirmation_dialog(self):
        # if not self.dialog:
        self.dialog = MDDialog(
            title="Enter Pin",
            type="custom",
            content_cls=Content(),
            buttons=[
                MDFlatButton(
                    text="cancel",on_release=self.callback
                ),
                MDRaisedButton(
                    text="[b]ok[/b]",
                    on_release=self.ok,
                    markup=True,

                ),
            ],
            size_hint_x=0.7,
            auto_dismiss=False,

        )
        self.dialog.open()

    def callback(self, *args):
        self.dialog.dismiss()

    def ok(self, *args):
        pin = Content().ids.pin.text

        if pin == "":
            toast("enter pin")

        else:
            toast(f"pin is {pin}")

【问题讨论】:

  • 你能发个minimal reproducible example吗?
  • 对不起。我是堆栈溢出的新手,这是我能给出的最小的例子。谢谢
  • 不要再次启动 Content 类,保存到 var 并传递它,它对我有用
  • @alfex4936 能否说明一下将文本保存到 var 以供传递的位置?

标签: kivy python-3.7 kivymd


【解决方案1】:

您可以将 Content 类调用到一个变量并在 MDDialog.content_cls 中使用它。

def show_custom_dialog(self):
    content_cls = Content() # call the class to a variable
    self.cdialog = MDDialog(content_cls=content_cls, 
           type='custom', title='Enter Pin'
            )
    self.cdialog.buttons = [
            MDFlatButton(text='cancel',on_release=self.close_dialog),
            MDRaisedButton(text='Ok',
            on_release=lambda x:self.get_data(x,content_cls))
            ]
    self.cdialog.open()

然后创建一个函数,通过在按钮中使用 lambda 表达式将 event_button 和内容类作为参数,如上所示。

def get_data(self,instance_btn, content_cls):
    textfield = content_cls.ids.pin
    # get input
    value = textfield._get_text()
    # do stufs here
    toast(value)

此时您可以提取 Content 类中的任何 id。我希望这会有所帮助。 完整代码见下文

from kivmd.app import MDApp
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDFlatButton, MDRaisedButtom
from kivy.lang.builder impor Builder
from kivy.toast import toast


kv = '''
MDBoxLayout:
    orientation : 'vertical'
    
    MDFlatbutton:
        text : 'Dialog'
        pos_hint : {'center_x':.5'}
        on_release : app.show_custom_dialog()

<Content>:
    MDTextField:
        id : pin
        pos_hint : {'center_x':.5,'center_y':.5}

'''

class Content(MDFloatLayout):
    pass


class InputDialogApp(MDApp):
    cdialog = None

    def build(self):
        return Builder.load_string(kv)

    def show_custom_dialog(self):
        content_cls = Content()
        self.cdialog = MDDialog(title='Enter Pin',
                 content_cls=content_cls,
                type='custom')
        self.cdialog.buttons = [
    
                MDFlatButton(text="Cancel",on_release=self.close_dialog), 

               MDRaisedButton(text="Ok",on_release=lambda x:self.get_data(x,content_cls))
                ]
        self.cdialog.open()

    def close_dialog(self, instance):
        if self.cdialog:
            self.cdialog.dismiss()

    def get_data(self, instance_btn, content_cls):
        textfield = content_cls.ids.pin
        value = textfield._get_text()
        # do stuffs here
        toast(value)
    
        self.close_dialog(instance_btn)

if __name__ == '__main__':
    InputDialogApp().run()

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
相关资源
最近更新 更多