【发布时间】:2020-05-06 08:17:13
【问题描述】:
如何将操作添加到“确定”按钮?我从 KivyMd 文档中获得了示例代码,但没有说明如何将操作添加到这些按钮。代码:
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:
hint_text: "City"
MDTextField:
hint_text: "Street"
FloatLayout:
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
),
MDFlatButton(
text="OK", text_color=self.theme_cls.primary_color
),
],
)
self.dialog.open()
Example().run()
单击“确定”后,我想从 MDTextField(城市和街道)中获取文本。我认为我应该为这些 MDTextFields 创建 ID,并使用 text="OK" 向 MDFlatButton 添加操作(on_release),但这对我没有帮助。如有任何建议,我将不胜感激。
【问题讨论】: