Mate
Mate

Reputation: 301

Kivy: How to update 'Label's' text with text from 'TextInput'?

I am making a GUI, in which there is a function generating Labels, and i want to give the user the possibility to change those Label's text as well.

But that was huge headache for me so i tried on a smaller scale i created a label which shows the amount of labels created and i tried to update the 'new amount labels text' with different solutions, but with no luck.

I tried threading but failed. Then i tried the Clock Object in Kivy, but i also failed, i didnt fail beacouse those dont work it is because I'm new to programing and i dont really undertand them. py:

class Screen_two(Screen):
    productsamount = StringProperty(None)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        Clock.schedule_once(self.update_amount_label, 0)

    def update_amount_label(self, dt):
        n = 0
        for i in db.databaseDict.items():
            n += 1
        self.ids.productsamount.text = 'Amount of goods: {}'.format(n)

kv:

<Screen_two>:
    productsamount: productsamount
    Label:
        font_size:'11dp'
        id:productsamount

Edit1: the def add_product(self): function is with which i would like to change the Labels text.

class addbut_popup(FloatLayout):
    addprodname = ObjectProperty(None)
    addprodprice = ObjectProperty(None)

    def print_to_consol(self):
        print('Added.')

    def add_product(self):
        if self.addprodname.text != "" and '.' not in self.addprodname.text:
            if re.findall(r'\D', self.addprodprice.text) == []:
                db.add_product(self.addprodname.text, self.addprodprice.text)
                self.print_to_consol()
                self.reset()
            else:
                invalid()
                self.reset()
        else:
            invalid()
            self.reset()

    def reset(self):
        self.addprodname.text = ''
        self.addprodprice.text = ''

Edit2:

I dont want to change the Label's text just once but i want to change it everytime a function is called (button pushed) for example: whenever i write something in a text input and i push a button i want to change the Label's text to what i wrote in the Textinput, and not just once but everytime when the button is pushed. (sorry for not making myself clear (Englsih is not my mother tongue))

Upvotes: 3

Views: 2045

Answers (4)

Emblay
Emblay

Reputation: 61

I think this should help

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import StringProperty, ObjectProperty
import random
from kivy.lang.builder import Builder


class YourWidget(Widget):
    def __init__(self, **kwargs):
        super(YourWidget, self).__init__(**kwargs)


    def change_text(self):
        self.label1.text = str(self.ids.input1.text)



Builder.load_string('''
<YourWidget>:
    input1:input1
    label1:label1
    BoxLayout:
        orientation: 'vertical'
        size: root.size
        TextInput:
            id: input1
        Label:
            id: label1
            text: ''
        Button:
            id: button1
            text: "Change text"
            on_release: root.change_text()

''')


class YourApp(App):
    def build(self):
        return YourWidget()


if __name__ == '__main__':
    YourApp().run()

this will help you understand it better

Upvotes: 1

Csaba Horvath
Csaba Horvath

Reputation: 73

This tutorial might help it is related to your question: https://www.youtube.com/watch?v=-NvpKDReKyg

Upvotes: 1

Martin
Martin

Reputation: 3385

Try something like this. You have button that has native support for events.

For example it has 'on_press' or 'on_release'. To these events you can attach function that will be triggered.

py:

class Screen_two(Screen):

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


    def update_amount_label(self):
        n = 0
        for i in db.databaseDict.items():
            n += 1
        self.ids.productsamount.text = 'Amount of goods: {}'.format(n)

kv:

<Screen_two>:
    productsamount: productsamount
    Label:
        font_size:'11dp'
        id:productsamount

    Button:
        on_press: root.update_amount_label()

ps: I didnt tested it but I think it should work

Upvotes: 1

John Anderson
John Anderson

Reputation: 39107

Here is a version of your code that I modified to do what you want:

from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.screenmanager import Screen, ScreenManager


class Screen_two(Screen):
    # StringProperty that will be the Label text
    productsamount = StringProperty('This is going to change')

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

        # change label after 5 seconds
        Clock.schedule_once(self.update_amount_label, 5)

    def update_amount_label(self, dt):
        n = 0
        for i in range(5):
            n += 1

        # update label text
        self.productsamount = 'Amount of goods: {}'.format(n)

Builder.load_string('''
<Screen_two>:
    Label:
        text: root.productsamount    # use the StringProperty for Label text
        font_size:'11dp'
''')

class tmpApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(Screen_two(name='screen2'))
        return sm

tmpApp().run()

Upvotes: 1

Related Questions