繁体   English   中英

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

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

我正在尝试使用python中的套接字和kivy制作聊天程序。 我为客户端编写了一个代码,当服务器发送消息时,接收该消息并更改TextInput的text属性,但是会发生此错误: AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'text'

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文件:

#: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)

我认为您只需要更改:

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

至:

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

ScreenMng类的textbox属性必须作为实例属性引用。 将其用作ScreenMng.textbox只是对ObjeectProperty本身的引用。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM