AttributeError: у объекта 'kivy.properties.ObjectProperty' нет атрибута 'text' - PullRequest
1 голос
/ 13 июня 2019

Я пытаюсь создать программу чата с использованием сокетов и кивы в Python.Я написал код для клиентской части, который, когда сервер отправляет сообщение, принимает это сообщение и изменяет текстовый атрибут TextInput, но возникает эта ошибка: 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 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)

1 Ответ

0 голосов
/ 13 июня 2019

Я думаю, вам просто нужно изменить:

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

на:

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

Свойство textbox класса ScreenMng должно упоминаться как свойство экземпляра.Использование его в качестве ScreenMng.textbox является просто ссылкой на сам ObjeectProperty.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...