简体   繁体   English

AttributeError:“ kivy.properties.ObjectProperty”对象没有属性“ text”

[英]AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'text'

I am trying to make a chat program with using sockets and kivy in python. 我正在尝试使用python中的套接字和kivy制作聊天程序。 I wrote a code for client side that when server sends a message, take that message and change the TextInput's text attribute but this error occurs: AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'text' 我为客户端编写了一个代码,当服务器发送消息时,接收该消息并更改TextInput的text属性,但是会发生此错误: AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'text'

gui.py file: gui.py文件:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
import client

def RecvMsg(DATA):
    # The error occurs in there
    ScreenMng.textbox.text += DATA + "\n"

class MainScreen(Screen):
    pass

class SecondScreen(Screen):
    pass

class ScreenMng(ScreenManager):
    textbox = ObjectProperty(None)

    def SendMsg(self, DATA):
        client.Client_Send(DATA)

    def GetIPNAME(self, IP, NAME):
        client.Connect(str(IP), str(NAME))

class MyApp(App):
    title= "CHAT"
    def build(self):
        return ScreenMng()

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

my.kv file: my.kv文件:

#:import FadeTransition kivy.uix.screenmanager.FadeTransition
#:import utils kivy.utils

<Main_Label@Label>
    font_size: 0.4 * root.height if root.height < root.width * .85 else 0.32 * root.width
    font_name: "Oswald-Medium.ttf"
    color: utils.get_color_from_hex("ca3e47")
<Main_TextInput@TextInput>
    font_size: 0.65 * root.height if root.height < root.width * .65 else 0.45 * root.width
<Main_Button@Button>
    font_size: 0.4 * root.height if root.height < root.width * .85 else 0.32 * root.width

<Button>
    font_name: "Oswald-Medium.ttf"
<Label>
    font_name: "Oswald-Medium.ttf"

<FillLabel@Label>

<ScreenMng>
    textbox: textbox
    transition: FadeTransition()
    canvas:
        Color:
            rgb: utils.get_color_from_hex("313131")
        Rectangle:
            size: root.size
            pos: root.pos
    MainScreen:
        GridLayout:
            cols: 1
            GridLayout:
                orientation: "right"
                cols: 2
                Main_Label:
                    text: "IP:"

                GridLayout:
                    padding: 20,0
                    rows: 3

                    FillLabel:
                    Main_TextInput:
                        id: ip
                        multiline: False
                    FillLabel:

                Main_Label:
                    text: "NAME:"

                GridLayout:
                    padding: 20,0
                    rows: 3
                    FillLabel:
                    Main_TextInput:
                        id: name
                        multiline: False
                    FillLabel:

            Main_Button:
                on_press:
                    root.GetIPNAME(ip.text, name.text)
                    root.current = "second"
                font_size: 0.3 * root.height if root.height < root.width else 0.3 * root.width
                text: "Submit"

    SecondScreen:
        name: "second"
        canvas:
            Color:
                rgb: utils.get_color_from_hex("313131")
            Rectangle:
                size: root.size
                pos: root.pos

        GridLayout:
            spacing: 30
            padding: 30,20
            rows: 2
            GridLayout:
                spacing: 20
                cols: 2
                TextInput:
                    id: textbox
                    text: ""
                    readonly: True

                TextInput:
                    text: "-ONLINE USERS-"
                    do_scroll_x: False
                    readonly: True
                    copy: False
                    size_hint_x: None
                    size: 200, 0

            GridLayout:
                cols: 2
                size_hint_y: None
                size: 0, 50
                spacing: 10

                TextInput:
                    id: message
                    size_hint_y: None
                    size: 0, 50
                    multiline: False

                Main_Button:
                    size_hint: None, None
                    size: 150, 50
                    text: "SEND"
                    on_press:
                        root.SendMsg(message.text)

I think you just need to change: 我认为您只需要更改:

ScreenMng.textbox.text += DATA + "\n"

to: 至:

App.get_running_app().root.textbox.text += DATA + "\n"

The textbox property of the ScreenMng class must be referenced as an instance property. ScreenMng类的textbox属性必须作为实例属性引用。 Using it as ScreenMng.textbox is just a reference to the ObjeectProperty itself. 将其用作ScreenMng.textbox只是对ObjeectProperty本身的引用。

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

相关问题 AttributeError: &#39;kivy.properties.ObjectProperty&#39; 对象没有属性 - AttributeError: 'kivy.properties.ObjectProperty' object has no attribute &#39;kivy.properties.ObjectProperty&#39; 对象没有属性 &#39;text&#39; - 'kivy.properties.ObjectProperty' object has no attribute 'text' AttributeError: 'kivy.properties.ObjectProperty' object 没有属性 'background_normal' - AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'background_normal' Python Kivy 错误:'kivy.properties.ObjectProperty' object 没有属性文本 - Python Kivy error: 'kivy.properties.ObjectProperty' object has no attribute text Python / kivy-TypeError:“ kivy.properties.ObjectProperty”对象不可迭代 - Python/kivy - TypeError: 'kivy.properties.ObjectProperty' object is not iterable AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39;kivy - AttributeError: 'NoneType' object has no attribute 'text' kivy Kivy AttributeError: object 没有属性 - Kivy AttributeError: object has no attribute Kivy Builder: AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;text&#39; - Kivy Buildozer: AttributeError: 'NoneType' object has no attribute 'text' Python-Kivy AttributeError: 'NoneType' object 没有属性 'text' - Python-Kivy AttributeError: 'NoneType' object has no attribute 'text' Kivy:AttributeError:&#39;...&#39;对象没有属性&#39;...&#39;,但它有 - Kivy: AttributeError: '...' object has no attribute '...', but it has
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM