Anyone who can help me. How to add a variable to the text of a MDLabel?

59 views
Skip to first unread message

Arnav Vats

unread,
Jul 24, 2021, 12:10:46 AM7/24/21
to Kivy users support
If this is my label how to make the text change according to the variable

MDLabel:
    id:label1
    text:"info"

Elliot Garbus

unread,
Jul 24, 2021, 12:36:26 AM7/24/21
to kivy-...@googlegroups.com

Here is an example of 3 ways to write text to a label.

There top button does the update all in kv.

The middle button used the id of the label to update the text property of the label.

The bottom button modifies a kivy property that is bound to the label text.

 

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty


kv =
"""
RootBoxLayout:
    orientation: 'vertical'
    Label:
        text: 'Write to a Label'
    BoxLayout:
        size_hint_y: None
        height: 48
        Label:
            id: label_1
            text: 'info'
        Button:
            text: 'update label'
            on_release:  label_1.text = 'Updated text, all in kv'
    BoxLayout:
        size_hint_y: None
        height: 48
        Label:
            id: label_2
            text: 'info'
        Button:
            text: 'update label'
            on_release:root.update_label_2()
    BoxLayout:
        size_hint_y: None
        height: 48
        Label:
            id: label
            text: root.label_3_text
        Button:
            text: 'update label'
            on_release: root.update_property()
"""


class RootBoxLayout(BoxLayout):
    label_3_text = StringProperty(
'info')

   
def update_label_2(self):
       
self.ids.label_2.text = 'updated text; python writes to label'

   
def update_property(self):
       
self.label_3_text = 'updated test, write to kivy property'


class WriteLabelApp(App):
   
def build(self):
       
return Builder.load_string(kv)


WriteLabelApp().run()

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5bf65b3b-2992-450e-88d4-2ff84b03dcffn%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages