How to modify the text of a button by pressing it?

1

I'm very new to this programming branch with graphical interfaces and I'm working on a project with Arduino ...

The communication part is OK, however I can not do something simple like: Change both the color and the text of the button at the same time.

If anyone can help, I appreciate it.

test.py:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.behaviors.button import ButtonBehavior
from kivy.uix.scrollview import ScrollView
from kivy.graphics import Color, Rectangle
from kivy.properties import ListProperty, StringProperty

class Portas(ScrollView):
    def __init__(self, portas, **kwargs):
        super().__init__(**kwargs)
        for onoff in portas:
            self.ids.box.add_widget(OnOff(text=onoff))

class Botao(ButtonBehavior, Label):
    btoff = ListProperty([1,0,0,0.4])
    bton = ListProperty([0,1,0,0.4])
    TextButton = StringProperty('Desligado')
    def __init__(self, **kwargs):
        super(Botao,self).__init__(**kwargs)
        self.atualizar()

    def atualizar(self, *args):
        self.canvas.before.clear()
        with self.canvas.before:
            Color(rgba=self.btoff)
            Rectangle(size=self.size,pos=self.pos)

    def on_pos(self, *args):
        self.atualizar()

    def on_size(self, *args):
        self.atualizar()

    def on_bton(self, *args):
        self.atualizar()

    def on_release(self, *args):
        self.btoff,self.bton = self.bton,self.btoff
        if(self.TextButton == 'Desligado'):
            self.TextButton = 'Ligado'
        else:
            self.TextButton = 'Desligado'
        print(self.TextButton)


class OnOff(BoxLayout):
    def __init__(self, text='', **kwargs):
        super().__init__(**kwargs)
        self.ids.label.text = text

test.kv:

<Portas>:
    BoxLayout:
        spacing:2
        id: box
        orientation: 'vertical'
        size_hint_y: None
        height: self.minimum_height

<OnOff>:
    size_hint_y:None
    heigth:100
    Label:
        id: label
        font_size:30
    Botao:
        id: button
        size_hint_x:None
        height: 28
        text: 'Desligado'
    
asked by anonymous 09.10.2018 / 22:22

0 answers