Search code examples
pythonkivykivy-language

How do I pass a string from my main.py to my .kv


I have a large string that I need to pass into my kv file so that I can print it as a label in the final app. Only problem is I don't know how. I have been looking online but I can't find a working solution that I can integrate into my code.

Here is the class containing the string ("lipoNames" is what I want to print as a label):

class RecordData(Screen):
    with open("Lipo names.txt") as f:
        lineList = f.readlines()
    lipoNames = ("".join(map(str, lineList)))

I have already opened the kv file outside the class using a builder since I am working with multiple menus. Below you will find a section of my kv file where the label will be placed:

<RecordData>
    name: "record"
    
    Label:
        text: ???
        font_size: (root.width**2 + root.height**2) / 13**4
        pos_hint:{"x": 0.325, "y": 0.86}
        size_hint:0.35, 0.15

This is my first project with kv so I am still very new to working with a kv file. Thank you for the help!


Solution

  • You can call root.yourvariable in kv language (it acts like self.yourvariable) and then in your relative class function call the Kivy StringProperty type to your variable so no type casting is needed and Kivy knows how to deal with the variable.

    Try this function below which updates the text every time you click the label:

    from kivy.uix.floatlayout import FloatLayout
    from kivy.uix.label import Label
    from kivy.app import App
    from kivy.properties import StringProperty
    from kivy.lang import Builder
    a = """
    <RecordData>
        Label:
            text: root.variabletext
            font_size: (root.width**2 + root.height**2) / 13**4
            size_hint:0.35, 0.15
            pos_hint:{"x": 0.325, "y": 0.86}
    """ 
    Builder.load_string(a)
    class RecordData(FloatLayout):
        variabletext = StringProperty("example")
        num = 0
        
        def on_touch_down(self, touch):
            self.variabletext = "We changed "+str(self.num)+" many times"
            self.num += 1
    
    class ExampleApp(App):
        def build(self):
            return RecordData()
            
    if __name__ == '__main__':
        ExampleApp().run()
    

    Similarly you could give the label an id in the KV language, then call the id and set the text property in your relative python class function, example below:

    from kivy.uix.floatlayout import FloatLayout
    from kivy.uix.label import Label
    from kivy.app import App
    from kivy.lang import Builder
    a = """
    <RecordData>
        Label:
            id: variabletext
            text: "example"
            font_size: (root.width**2 + root.height**2) / 13**4
            size_hint:0.35, 0.15
            pos_hint:{"x": 0.325, "y": 0.86}
    """ 
    Builder.load_string(a)
    class RecordData(FloatLayout):
        num = 0
        
        def on_touch_down(self, touch):
            self.ids.variabletext.text = "We changed "+str(self.num)+" many times"
            self.num += 1
    
    class ExampleApp(App):
        def build(self):
            return RecordData()
            
    if __name__ == '__main__':
        ExampleApp().run()