【问题标题】:How to dynamically update markup text in kivy modalview如何在kivy modalview中动态更新标记文本
【发布时间】:2018-02-22 14:30:35
【问题描述】:

我正在尝试从 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()

【问题讨论】:

    标签: python python-2.7 kivy modal-view


    【解决方案1】:

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

    【讨论】:

    • 哇!非常感谢您非常快速和出色的回答。你让我今天一整天都感觉很好。 :)
    猜你喜欢
    • 2017-10-08
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多