Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

kivy

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))

like image 368
Mate Avatar asked Dec 02 '25 10:12

Mate


1 Answers

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

like image 83
Emblay Avatar answered Dec 06 '25 00:12

Emblay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!