Search code examples
pythonkivykivy-languagekivymd

How do I access a variable from my main.py in my .kv file


from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.label import Label


Balance = 0
Balance_string = str(Balance)

class MyWidget(Widget):
    def ads(self):
        global Balance
        Balance += 0.25
        Balance_string = str(Balance)
        print(Balance_string)
        return Balance_string

class BuzzMoneyApp(App):
    def build(self):
        return MyWidget()

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

** my .kv file **

<MyWidget>:
    canvas:
        Color:
            rgb: (0, 150,0)
        Rectangle:
            pos: self.pos
            size: self.size

    Button:
        center: self.parent.center
        font_size: 14
        height: 28
        background_color: (1.0, 0.0, 0.0, 1.0)
        text: "Earn money"
        on_press:root.ads()

I want to access the Balance string variable from my main.py in my .kv file, so that I can display it on my screen.


Solution

  • You can easily reference Properties from your python inside your kv. Here is a modified version of your code that does that:

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.properties import NumericProperty, StringProperty
    from kivy.uix.widget import Widget
    
    # Balance = 0
    # Balance_string = str(Balance)
    
    class MyWidget(Widget):
        Balance = NumericProperty(0)
        Balance_string = StringProperty('0')
    
        def ads(self):
            self.Balance += 0.25
            print(self.Balance_string)
    
        def on_Balance(self, *args):
            # update Balance_string whenever Balance changes
            self.Balance_string = str(self.Balance)
    
    class BuzzMoneyApp(App):
        def build(self):
            return MyWidget()
    
    if __name__ == '__main__':
        BuzzMoneyApp().run()
    

    And then you can reference those Properties in the kv:

    <MyWidget>:
        canvas:
            Color:
                rgb: (0, 150,0)
            Rectangle:
                pos: self.pos
                size: self.size
    
        Button:
            center: self.parent.center
            font_size: 14
            height: 28
            background_color: (1.0, 0.0, 0.0, 1.0)
            text: "Earn money"
            on_press:root.ads()
        Label:
            text: root.Balance_string  # root.Balance_string can be replaced with just str(root.Balance)
            size_hint: None, None
            size: self.texture_size
            pos: 300, 200
            color: 0,0,0,1
    

    The Properties must be defined within an EventDispatcher (typically a Widget). The on_Balance() method is automatically triggered whenever the Balance Property changes, and it updates the Balance_string Property. The Balance_string can be used within the kv as shown, and the Label will be updated whenever the Balance_string Property changes. Since the Balance_string is just the string value of Balance, it can be eliminated and replaced with str(root.Balance) in the kv.