fare
fare

Reputation: 43

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

I am trying to make a chat program with using sockets and kivy in python. 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'

gui.py file:

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:

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

Upvotes: 4

Views: 2593

Answers (1)

John Anderson
John Anderson

Reputation: 38822

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. Using it as ScreenMng.textbox is just a reference to the ObjeectProperty itself.

Upvotes: 3

Related Questions