简体   繁体   English

Python Kivy 错误:'kivy.properties.ObjectProperty' object 没有属性文本

[英]Python Kivy error: 'kivy.properties.ObjectProperty' object has no attribute text

I've just started to learn Python Kivy and want to output text from TextInput Error in console: AttributeError: 'kivy.properties.ObjectProperty' object has no attribute text I've just started to learn Python Kivy and want to output text from TextInput Error in console: AttributeError: 'kivy.properties.ObjectProperty' object has no attribute text

My Python Code:我的 Python 代码:

class SearchField(AnchorLayout):
    search_field = ObjectProperty(None)
    result = search_field.text
class MyApp(App):
    def build(self):
        return SearchField()
if __name__ == "__main__":
    MyApp().run()

My.kv code:我的.kv代码:

<SearchField>
    search_field: search_field
    anchor_x: "center"
    anchor_y: 'top'
    padding: (0, 20)
    BoxLayout:
        size_hint: (None, None)
        size: (600, 30)
        TextInput:
            id: search_field
            multiline: False
        Button:
            size_hint: (None, None)
            size: (50, 30)
            text: "Search"
            on_release: search_result.text = root.result
    Label:
         id: search_result
         text: ""
         font_size: 30

Several errors are made in this program.这个程序有几个错误。 1- the text attribute is applied to the search_field variable directly after its declaration as ObjectProperty. 1- text属性在声明为 ObjectProperty 之后直接应用于 search_field 变量。 So, it's an ObjectProperty instance and dont have the attribute text .所以,它是一个 ObjectProperty 实例并且没有属性text 2- The indentation of the program is badly done. 2-程序的缩进做得不好。 3- The properties are not well declared in the.kv file. 3- 在.kv 文件中没有很好地声明属性。

Although I haven't fully understood the purpose of the program, here is a safe version:虽然我还没有完全理解程序的目的,但这里有一个安全的版本:

from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.properties import ObjectProperty


class SearchField(AnchorLayout):
    search_field = ObjectProperty(None)
    result = ""

    def btn(self):
        self.result = self.search_field.text
        print(self.result)


class TestApp(App):
    def build(self):
        return SearchField()


if __name__ == "__main__":
    TestApp().run()

And here is the kv file:这是kv文件:

<SearchField>
    search_field: search_field
    anchor_x: "center"
    anchor_y: 'top'
    padding: (0, 20)
    search_result: search_result
    search_field: search_field
    BoxLayout:
        size_hint: (None, None)
        size: (600, 30)
        TextInput:
            id: search_field
            multiline: False
        Button:
            size_hint: (None, None)
            size: (50, 30)
            text: "Search"
            on_release: root.btn()
    Label:
        id: search_result
        text: ""
        font_size: 30

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

相关问题 &#39;kivy.properties.ObjectProperty&#39; 对象没有属性 &#39;text&#39; - 'kivy.properties.ObjectProperty' object has no attribute 'text' AttributeError:“ kivy.properties.ObjectProperty”对象没有属性“ text” - AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'text' AttributeError: &#39;kivy.properties.ObjectProperty&#39; 对象没有属性 - AttributeError: 'kivy.properties.ObjectProperty' object has no attribute Python / kivy-TypeError:“ kivy.properties.ObjectProperty”对象不可迭代 - Python/kivy - TypeError: 'kivy.properties.ObjectProperty' object is not iterable AttributeError: 'kivy.properties.ObjectProperty' object 没有属性 'background_normal' - AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'background_normal' 模块“kivy.properties”中没有名称“ObjectProperty” - Python Kivy - No name 'ObjectProperty' in module 'kivy.properties' - Python Kivy Kivy错误:属性错误:&#39;无类型&#39;对象没有属性&#39;text&#39; - Kivy error: Attribute error:'None type' object has no attribute 'text' Kivy“对象没有属性”错误 - Kivy 'object has no attribute' Error Python-Kivy AttributeError: 'NoneType' object 没有属性 'text' - Python-Kivy AttributeError: 'NoneType' object has no attribute 'text' AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39;kivy - AttributeError: 'NoneType' object has no attribute 'text' kivy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM