0
Вот код. Мне нужно чтобы в классе ClientProtocol можно было менять текст кнопки класса Chat. 
import asyncio
from asyncio import transports
from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window 
from kivy.uix.button import Button
from kivy.uix.label import Label


Window.size = (400,800)




class ClientProtocol(asyncio.Protocol,BoxLayout):
    transport: transports.Transport

    def __init__(self, **kwargs):
        super().__init__(**kwargs)



    def data_received(self, data: bytes):
        print(data)
        decoded = data.decode()
        self.label.text  = decoded

    def connection_made(self, transport: transports.Transport):
        self.label.text = ("Успешно подключились, введите логин")
        self.transport = transport

    def connection_lost(self, exception):
        self.label.text("Вы отключены от сервера")


class Chat(BoxLayout):
    protocol: ClientProtocol

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.button.text = "xxxl"

    def send_message(self):
        message = self.button.text()

        self.protocol.transport.write(message.encode())

    def create_protocol(self):
        self.protocol = ClientProtocol()
        return self.protocol



    async def start(self):


        loop = asyncio.get_running_loop()

        coroutine = loop.create_connection(
            self.create_protocol,

        )

        await asyncio.wait_for(coroutine, 1000)


class MyApp(App):
    def build(self):
        return Chat()



MyApp().run()

process = Chat()

asyncio.run(process.start())




loop = asyncio.new_event_loop()
loop.run_forever()


KV файл

<Chat>:
    button:button
    BoxLayout:
        orientation:"vertical"


        TextInput:
            size_hint: 1,1

        Button:
            id:button
            text: "Send"
            size_hint:1,0.3

<ClientProtocol>:
    label:label
    Label:
        id:label
        text:"d"    

2 ответа 2

0
instance.ids.button_id.text = "New text"
1
  • Не работает( Текст кнопки не изменяется
    – илья
    14 мая 2020 в 17:51
0

Я показал принцип. Ничего же сложного нет.

from kivy.lang import Builder
from kivy.app import App


KV = """
BoxLayout:

    Button:
        id: btn_1
        text: "Text"
        on_release: btn_2.text = "New text"

    Button:
        id: btn_2
        text: "Text"
        on_release: btn_1.text = "New text"
"""


class MainApp(App):
    def build(self):
        return Builder.load_string(KV)


MainApp().run()

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.