简体   繁体   English

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

[英]How to dynamically update markup text in kivy modalview

I am trying to update a label field dynamically from the contents of a TextInput in a ModalView. 我正在尝试从ModalView中TextInput的内容动态更新标签字段。 The idea is that in the TextInput one enters plain text including markup formatting and you will see the results directly in the Label field with markup = True. 这个想法是,在TextInput中输入纯文本,包括标记格式,您将在带有markup = True的Label字段中直接看到结果。

Unfortunately I do not know how to access the Label item in the ModalView. 不幸的是,我不知道如何访问ModalView中的Label项。 Who can help? 谁能帮忙? See the example code below. 请参见下面的示例代码。

Thanks in advance. 提前致谢。

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()

You have to make a binding between the TextIntput text and the Label , for this we can use a lambda function and setattr . 您必须在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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM