Skip to content Skip to sidebar Skip to footer

How To Change Text Of A Label In The Kivy Language With Python

I was wondering how I could change the text of a label made inside the Kivy language using Python. Like how would I have user input from python be made as the text of a label in ki

Solution 1:

Text of a label can be a kivy property, which can be later changed and since it is a kivy property it will automatically updated everywhere. Here is an example of your .py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import StringProperty
import random

classYourWidget(Widget):
    random_number = StringProperty()

    def__init__(self, **kwargs):
        super(YourWidget, self).__init__(**kwargs)
        self.random_number = str(random.randint(1, 100))

    defchange_text(self):
        self.random_number = str(random.randint(1, 100))

classYourApp(App):
    defbuild(self):
        return YourWidget()

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

and your .kv

<YourWidget>:BoxLayout:size:root.sizeButton:id:button1text:"Change text"on_release:root.change_text()Label:id:label1text:root.random_number

When you click the button, it will call change_text() function, which will randomly change the text of the label to random integer between 1 and 100.

Post a Comment for "How To Change Text Of A Label In The Kivy Language With Python"