从 MDDialog 的 content_cls 访问文本字段的文本并将其传递给函数
Accessing text of textfield from content_cls of MDDialog and pass it to a function
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
hint_text: "City"
MDTextField:
id: street
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", text_color=self.theme_cls.primary_color,
on_press= lambda x, city_name = self.dialog.content_cls.ids.city.text : self.print_city(city_name)
),
MDFlatButton(text = "print",
on_press= lambda x, street_name= self.dialog.content_cls.ids.city.text : self.print_street(street_name)
),
],
)
self.dialog.open()
def print_city(city_name):
print(city_name)
def print_street(street_name):
print(street_name)
Example().run()
如何从 self.dialog.content_cls.ids.city.text 获取文本并将其传递给 self.print_city
有可能吗
如果没有,请告诉我另一种完成方法
我正在尝试创建计费软件。通过 MDDialog 获取用户输入的数量值
提前致谢
抱歉损坏的工程
问题是您在对话框实例化期间传递值。这可能会导致错误。您实际需要做的是在创建对话框后传递值(此处是用户按下 'Print' 时)。
换行,
MDFlatButton(text = "[b]Print",
on_press= lambda x, street_name= self.dialog.content_cls.ids.city.text : self.print_street(street_name)
),
与以下,
MDFlatButton(text = "[b]Print",
on_press=self.print_street,
),
然后在print_street
,
def print_street(self, btn):
street_name = self.dialog.content_cls.ids.street.text
print(street_name)
其他类似。
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
hint_text: "City"
MDTextField:
id: street
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", text_color=self.theme_cls.primary_color,
on_press= lambda x, city_name = self.dialog.content_cls.ids.city.text : self.print_city(city_name)
),
MDFlatButton(text = "print",
on_press= lambda x, street_name= self.dialog.content_cls.ids.city.text : self.print_street(street_name)
),
],
)
self.dialog.open()
def print_city(city_name):
print(city_name)
def print_street(street_name):
print(street_name)
Example().run()
如何从 self.dialog.content_cls.ids.city.text 获取文本并将其传递给 self.print_city
有可能吗 如果没有,请告诉我另一种完成方法
我正在尝试创建计费软件。通过 MDDialog 获取用户输入的数量值
提前致谢 抱歉损坏的工程
问题是您在对话框实例化期间传递值。这可能会导致错误。您实际需要做的是在创建对话框后传递值(此处是用户按下 'Print' 时)。
换行,
MDFlatButton(text = "[b]Print",
on_press= lambda x, street_name= self.dialog.content_cls.ids.city.text : self.print_street(street_name)
),
与以下,
MDFlatButton(text = "[b]Print",
on_press=self.print_street,
),
然后在print_street
,
def print_street(self, btn):
street_name = self.dialog.content_cls.ids.street.text
print(street_name)
其他类似。