如何解决尝试在 MDTextField (KivyMD) 中访问后未显示用户文本
在使用 KivyMD 在 mddialog 中创建 MDTextField 后,我尝试访问用户在 MDTextField 中输入的文本并将标签的文本更改为输入的任何内容,但无济于事。这是我的代码如下:
# importing all the necessary modules and inheriting my dialog from Boxlayout
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.Boxlayout import BoxLayout
from kivymd.uix.dialog import mddialog
# creating a string which will contain all my kv deFinitions that I would put in a .kv file
the_kv_file = '''
<ContentOfAppointmentDialog>:
orientation: "vertical"
spacing: "10dp"
size_hint: None,None
size: "250dp","150dp"
MDTextField:
hint_text: "Enter details"
id: the_textfield
helper_text: "Be detailed and precise"
helper_text_mode: "on_focus"
max_text_length: 75
multiline: True
mode: "rectangle"
MDFlatButton:
text: "disCARD"
on_release: app.CloseDialog()
MDFlatButton:
text: "DONE"
on_release: app.ChangingLabel()
MDFloatLayout:
MDLabel:
id: label_to_change
text: "hello"
MDFloatingActionButton:
icon: 'pencil'
on_press: app.OpenDialog()
'''
# Creating the classes of My main app and the dialog as well as the required functions
class ContentOfAppointmentDialog(BoxLayout): # this defines the dialogs layout
pass
class MainApp(MDApp): # my main app
def build(self):
self.main_screen = Builder.load_string(the_kv_file)
self.dialog = mddialog(title="my example",type="custom",content_cls=ContentOfAppointmentDialog()) # defining the dialog
self.AccessDialog = ContentOfAppointmentDialog()
return self.main_screen
def OpenDialog(self): # opens dialog
self.dialog.open()
def CloseDialog(self): # closes dialog
self.dialog.dismiss()
# this is the function that is not working as I intended it to
def ChangingLabel(self): # function that is supposed to change the label to what users input is
self.main_screen.ids.label_to_change = self.AccessDialog.ids.the_textfield.text
self.dialog.dismiss()
MainApp().run()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。