StringProperty的TextInput的奇异引用文本 [英] kivy reference text of TextInput by StringProperty

查看:55
本文介绍了StringProperty的TextInput的奇异引用文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过StringProperty获取TextInput的文本,但是它不起作用.我得到一个空字符串.在第二个示例中,我将整个TextInput声明为ObjectProperty,然后它确实起作用.我的第一个示例出了什么问题?

I would like to get the text of my TextInput via a StringProperty, but it does not work. I get an empty string. In the second example, I am declaring the whole TextInput as an ObjectProperty and then it does work. What is wrong with my first example?

第一个示例不打印TextInput的文本 example1.py

First example does not print text of TextInput example1.py

from kivy.app import App
from kivy.base import Builder
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout

Builder.load_string("""
<rootwi>:
    orientation: 'vertical'
    Button:
        on_press: root.print_txt()
    TextInput:
        text: root.textinputtext
""")
class rootwi(BoxLayout):
    textinputtext = StringProperty()

    def print_txt(self):
        print(self.textinputtext)


class MyApp(App):
    def build(self):
        return rootwi()

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

第二个示例确实打印了TextInput的文本,但使用的是ObjectProperty而不是StringProperty example2.py

Second example does print text of TextInput, but uses a ObjectProperty not StringProperty example2.py

from kivy.app import App
from kivy.base import Builder
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock


Builder.load_string("""
<rootwi>:
    txt: txt
    orientation: 'vertical'
    Button:
        on_press: root.print_txt()
    TextInput:
        id: txt
""")
class rootwi(BoxLayout):
    txt = ObjectProperty()

    def print_txt(self):
        print(self.txt.text)


class MyApp(App):
    def build(self):
        return rootwi()

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

如果我将文本设置为特定于某事物,它将显示在TextInput中.(但仍然无法打印)

from kivy.app import App
from kivy.base import Builder
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout

Builder.load_string("""
<rootwi>:
    orientation: 'vertical'
    Button:
        on_press: root.print_txt()
    TextInput:
        text: root.textinputtext
""")
class rootwi(BoxLayout):
    textinputtext = StringProperty()

    def __init__(self, **kwargs):
        self.textinputtext = 'palim'
        super(rootwi, self).__init__(**kwargs)

    def print_txt(self):
        print(self.textinputtext)


class MyApp(App):
    def build(self):
        return rootwi()

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

推荐答案

如果要使用StringProperty设置并获取文本,则应创建双向绑定:

If you want set and get the text using the StringProperty then you should create a bidirectional bind:

from kivy.app import App
from kivy.base import Builder
from kivy.properties import StringProperty, ObjectProperty
from kivy.uix.boxlayout import BoxLayout


Builder.load_string("""
<rootwi>:
    orientation: 'vertical'
    textinputtext: txt.text
    Button:
        on_press: root.print_txt()
    TextInput:
        id: txt
        text: root.textinputtext
""")

class rootwi(BoxLayout):
    textinputtext = StringProperty()

    def __init__(self, **kwargs):
        super(rootwi, self).__init__(**kwargs)
        self.textinputtext = 'palim'

    def print_txt(self):
        print(self.textinputtext)



class MyApp(App):
    def build(self):
        return rootwi()

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

输出:

这篇关于StringProperty的TextInput的奇异引用文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆