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

2024-09-30 12:27:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从ModalView中TextInput的内容动态更新标签字段。其思想是在TextInput中输入纯文本(包括标记格式),您将直接在markup=True的Label字段中看到结果。在

不幸的是,我不知道如何访问ModalView中的标签项。谁能帮忙?请参阅下面的示例代码。在

提前谢谢。在

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

Tags: textfromimportselfnonesizeondef
1条回答
网友
1楼 · 发布于 2024-09-30 12:27:51

您必须在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)

enter image description here

相关问题 更多 >

    热门问题