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

Asked

Viewed 494 times

1

I am very new in this field of programming with graphical interfaces and I am with a project with Arduino...

The communication part is OK, but I can’t do anything simple like: Change both the color and text of the button at the same time.

If anyone can help I’d 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'
  • Vitão, which programming language you are using?

  • Python 3.7 and Kivy

  • try calling update function()

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.