如何在kivy modalview中动态更新标记文本

How to dynamically update markup text in kivy modalview

我正在尝试根据 ModalView 中 TextInput 的内容动态更新标签字段。这个想法是,在 TextInput 中输入包括标记格式的纯文本,您将直接在标记 = True 的标签字段中看到结果。

很遗憾,我不知道如何访问 ModalView 中的 Label 项。谁能帮忙?请参阅下面的示例代码。

提前致谢。

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.modalview import ModalView
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.properties import ObjectProperty, StringProperty

kv = """
<Test>:
    canvas:
        Color:
            rgba: 0.4, 0.5, 0.6, 1
        Rectangle:
            size: self.size
            pos: self.pos
    Button:
        size_hint: None, None
        size: 3 * dp(48), dp(48)
        text: 'Edit'
        on_press: root.showedit()
"""

Builder.load_string(kv)


class Test(BoxLayout):
    minput_text = StringProperty('Welcome')
    txtresult = ObjectProperty()

    def showedit(self):
        mview = ModalView(id='mviewid', size_hint=(0.4, 0.6), auto_dismiss=False, background='./images/noimage.png')
        mblt = BoxLayout(orientation='vertical', padding=(24))
        minp = TextInput(id='inptxt', text='', hint_text='Start typing text with markup here', size_hint=(1,0.5),multiline=True)
        minp.bind(text=self.on_inptext)

        mtxt = Label(id='txtresult',text='displays formatted text', color=(0.3,0.3,0.3), size_hint=(1,0.5),markup=True)
        mcnf = Button(text='OK', size=(144,48), size_hint=(None,None))
        mcnf.bind(on_press=mview.dismiss)

        mblt.add_widget(minp)
        mblt.add_widget(mtxt)
        mblt.add_widget(mcnf)
        mview.add_widget(mblt)
        mview.bind(on_dismiss=self.print_text)
        mview.open()

    def on_inptext(self, instance, value):
        self.minput_text = value

    def print_text(self, *args):
        print self.minput_text


class TestApp(App):
    def build(self):
        return Test()


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

您必须在 TextIntput 文本和 Label 之间进行绑定,为此我们可以使用 lambda 函数和 setattr.

class Test(BoxLayout):
    minput_text = StringProperty('Welcome')
    txtresult = ObjectProperty()

    def showedit(self):
        mview = ModalView(id='mviewid', size_hint=(0.4, 0.6), auto_dismiss=False, background='./images/noimage.png')
        mblt = BoxLayout(orientation='vertical', padding=(24))
        minp = TextInput(id='inptxt', text='', hint_text='Start typing text with markup here', size_hint=(1,0.5),multiline=True)
        minp.bind(text=self.on_inptext)

        mtxt = Label(id='txtresult',text='displays formatted text', color=(0.3,0.3,0.3), size_hint=(1,0.5),markup=True)
        mcnf = Button(text='OK', size=(144,48), size_hint=(None,None))
        mcnf.bind(on_press=mview.dismiss)

        mblt.add_widget(minp)
        mblt.add_widget(mtxt)
        mblt.add_widget(mcnf)
        mview.add_widget(mblt)
        mview.bind(on_dismiss=self.print_text)
        # binding between TextInput text and Label text
        minp.bind(text=lambda instance, value: setattr(mtxt, 'text',value))

        mview.open()

    def on_inptext(self, instance, value):
        self.minput_text = value

    def print_text(self, *args):
        print(self.minput_text)