如何在kivymd的弹出MDTextField中获取文本

问题描述

大家都好吗?这是我关于 StackOverflow 的第一个问题。

我正在学习 Kivy,但无法在弹出窗口中输入文本。目标是将文本放入变量中。

我的代码非常大,所以我将创建一个完全复制我的场景的基本示例。走吧。

这是我的代码.py

from Produtivese_Key_Mouse import Record_mk_pyp
from kivy.core.window import Window
from kivymd.app import MDApp
from kivymd.uix.Boxlayout import MDBoxLayout
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import mddialog

Window.size = (800,500)

class Class_one(MDBoxLayout):
    
    conter = 0
    
    def __init__(self,task=[],**kwargs):
        super().__init__(**kwargs)
        Class_one.add_tasks(self,task)
    
    def add_tasks(self,task,**kwargs):
        for x in task:
            Class_one.conter += 1
            cont = Class_one.conter
            
            icon_left = f'{x["icon"]}'
            title = f'{cont} - {x["tx1"]}'
            second_title = f'{x["tx2"]}'
            third_title = f'{x["tx3"]}'

            self.ids.Box_one.add_widget(Join_tasks_buttons(icon=icon_left,tx1=title,tx2=second_title,tx3=third_title)
                                                          )

    def callback_one(self,instance):
        if instance.icon == 'format-text':
            self.dialog = None
            Class_one.popup_insert_text(self)
        
        if instance.icon == 'language-rust': 
            my_data = [{'icon': 'language-python','tx1': 'Python','tx2': 'Hello','tx3': 'World'}]
            Class_one.add_tasks(self,my_data)


    def popup_insert_text(self,**kwargs):
        out_button = MDFlatButton(text='Go out',on_release=self.close_popup)
        send_button = MDFlatButton(text='Submit',on_release=self.send_text)
        if not self.dialog:
            self.dialog = mddialog(title='Thanks for helping',type="custom",content_cls=My_text(),buttons=[out_button,send_button]
                                   )
        self.dialog.open()
    
    # Here is my problem,I can't put the typed text in the variable (Where_is_the_text)
    def send_text(self,text,**kwargs):
        Where_is_the_text = My_text().ids.texto_inserido.text
        print(f'o texto é: {Where_is_the_text}')

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


class Join_tasks_buttons(MDBoxLayout):
    def __init__(self,icon='',tx1='',tx2='',tx3='',**kwargs):
        super().__init__(**kwargs)
        self.ids.evento_icone.icon = icon
        self.ids.evento_texto_title.text = tx1
        self.ids.evento_texto_second.text = tx2
        self.ids.evento_texto_third.text = tx3


class My_text(MDBoxLayout):
    pass


class TestApp(MDApp):
    
    def build(self):
        self.theme_cls.primary_palette = 'Blue'
        self.theme_cls.primary_hue = '300'
        self.theme_cls.theme_style = 'Light'
        
        return Class_one([{'icon': 'language-python','tx3': 'World'}])
    
    
if __name__ == '__main__':
    TestApp().run()

这是我的代码 .kv (test.kv)

#:kivy 2.0.0
#:import get_color_from_hex kivy.utils.get_color_from_hex

<Class_one>:
    orientation: 'vertical'
    padding: "1dp"

    ScrollView:
        MDBoxLayout:
            id: Box_one
            orientation: 'vertical'
            adaptive_height: True
            padding: "1dp"

    MDBoxLayout:
        size_hint_y: None
        height: 30

        MDFloatingActionButtonSpeedDial:
            right_pad: True
            hint_animation: True
            callback: root.callback_one
            data:
                {'Hello': 'format-text',\
                'Hi': 'language-rust'}
            icon: 'alpha-r-Box'
            root_button_anim: True
            auto_dismiss: True
            dismiss_on_option_touch: True

<Join_tasks_buttons>:
    size_hint_y: None
    height: dp(88)
    canvas.before:
        Color:
            rgba: get_color_from_hex("#BDBDBD")
        Line:
            width: 0.5
            rectangle: self.x,self.y,self.width,self.height
    MDIcon:
        id: evento_icone
        size_hint: 0.07,1
        halign: "center"

    MDBoxLayout:
        orientation: 'vertical'
        size_hint: 0.7,1
        MDLabel:
            id: evento_texto_title
            font_style: 'Subtitle1'
            theme_text_color: 'Primary'
            size_hint: 1,0.3
        MDLabel:
            id: evento_texto_second
            font_style: 'Body1'
            theme_text_color: 'Secondary'
            size_hint: 1,0.2
        MDLabel:
            id: evento_texto_third
            font_style: 'Body1'
            theme_text_color: 'Secondary'
            size_hint: 1,0.5

    MDBoxLayout:
        adaptive_width: True
        MDIconButton:
            icon: 'chevron-up'
            halign: "center"
            pos_hint: {"center_x": .5,"center_y": .5}
        MDIconButton:
            icon: 'delete-outline'
            halign: "center"
            pos_hint: {"center_x": .5,"center_y": .5}
            on_release:app.root.ids.Box_one.remove_widget(root)
        MDIconButton:
            icon: 'chevron-down'
            halign: "center"
            pos_hint: {"center_x": .5,"center_y": .5}

<My_text>
    orientation: "vertical"
    spacing: '12dp'
    size_hint_y: None
    height: '80dp'
    MDTextField:
        id: texto_inserido
        text: 'How to change this text????'
        hint_text: 'Type the text here:'
        helper_text: 'Do not forget to click on send to send the typed text.'
        helper_text_mode: 'on_focus'
        icon_right: "format-text"
        icon_right_color: app.theme_cls.primary_color
        pos_hint: {'center_x': 0.5,'center_y': 0.5}
        multiline: True
        max_height: "90dp"

操作: 单击 R 按钮和 T 按钮打开弹出窗口

感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)