0

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

不幸的是,我不知道如何访问 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()
4

1 回答 1

1

您必须在TextIntputtext 和之间进行绑定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)

在此处输入图像描述

于 2018-02-22T14:59:30.833 回答