Gacut
Gacut

Reputation: 47

How can i update label text by pressing button

I have a question that i can't seem to find an anwser. I have this label:

l = Label(text='Some Text', font_size=100)

I have also binded text input to 'l' that looks like this:

t = TextInput(font_size=80, size_hint_y=None, height=200, text='Time', halign='right')
t.bind(text=l.setter('text'))

So when i type something to my text input box, it instantly updates the label and shows result on screen. But i want to update that text only when user presses 'Add' button. I am new to kivy and i am still experiment with it. Any help would be great. Thanks!

@edit here is my code:

https://gist.githubusercontent.com/Gacut/d765231c2696831af8c8a3315fdabbfd/raw/e1bdcdd8aca588b1c9268e76f7a47fa7576d54cb/gistfile1.txt

and what i am trying to do, is this kind of app, but for android:

https://wumpa.app/

Upvotes: 0

Views: 157

Answers (1)

John Anderson
John Anderson

Reputation: 39107

The

t.bind(text=l.setter('text'))

will do exactly what you describe, so that is not what you want. Instead, use the Button on_release property to call a method that does what you want. The simplest way to do that is to save references to the Widgets involved (t and lista), and use those references in the new method. Like this:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button


class WumpaTime(App):
    def build(self):
        layout = BoxLayout(orientation='vertical')
        title = Label(text='Wumpa Time Countdown', size_hint_y=None, height=100, )
        self.t = TextInput(font_size=80, size_hint_y=None, height=200, halign='right')
        self.lista = Label(font_size=100)
        box = BoxLayout()
        box2 = BoxLayout()
        bremove = Button(text="Remove", size_hint=(None, None), size=(100, 100))
        badd = Button(text="Add", size_hint=(None, None), size=(100, 200), on_release=self.update_label)

        #t.bind(text=lista.setter('text'))

        box.add_widget(self.t)
        box.add_widget(badd)
        layout.add_widget(title)
        layout.add_widget(self.lista)
        layout.add_widget(box)

        return layout

    def update_label(self, button_instance):
        self.lista.text = self.t.text


WumpaTime().run()

Upvotes: 1

Related Questions