Python Forum
[Kivy] Kivy property (in .kv) loses binding to a variable (in .py) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Kivy] Kivy property (in .kv) loses binding to a variable (in .py) (/thread-12172.html)



Kivy property (in .kv) loses binding to a variable (in .py) - j.crater - Aug-12-2018

Hello all,
so, I have the following files:

main.py
from kivy.app import App

import parameters


class MainApp(App):
    gui_data = parameters.Data()

    def increment(self):
        self.gui_data.number = str(int(self.gui_data.number) + 1)

    def decrement(self):
        self.gui_data.number = str(int(self.gui_data.number) - 1)

    def reset_parameters(self):
        self.gui_data = parameters.Data()


if __name__ == '__main__':
    MainApp().run()
parameters.py
from kivy.properties import StringProperty
from kivy.event import EventDispatcher


class Data(EventDispatcher):
    number = StringProperty("0")
    # number = StringProperty()

    # def __init__(self):
    #     self.number = "0"
main.kv
BoxLayout:
    orientation: "vertical"

    Label:
        text: app.gui_data.number

    Button:
        text: "Increment"
        on_press: app.increment()

    Button:
        text: "Decrement"
        on_press: app.decrement()

    Button:
        text: "Reset parameters"
        on_press: app.reset_parameters()
I know there are shortcuts to implement what the program above is doing, but I am trying to make a skeleton for a larger piece of code.
increment() and decrement() functions alter the number variable, which in turn effects what text label on GUI shows.
But I would like to be able to reset the parameters to default. I thought this could simply be done by reassigning a Data() class to gui_data variable in application class. This would reset the variables to defaults (imagining there will be many), while the .kv keeps the reference to the same gui_data object. However, since reassigning (reset_parameters())the text label doesn't return to the number's default value, nor responds to button presses.
I also tried with class' init method (in comments), but outcome was same.

What am I missing here, why does label/text in .kv (seem to) lose reference to gui_data object?

Thanks,
JC


RE: Kivy property (in .kv) loses binding to a variable (in .py) - buran - Aug-14-2018

from kivy.app import App
 
import parameters
 
class MainApp(App):
    gui_data = parameters.Data()
 
    def increment(self):
        self.gui_data.number = str(int(self.gui_data.number) + 1)
 
    def decrement(self):
        self.gui_data.number = str(int(self.gui_data.number) - 1)
 
    def reset_parameters(self):
        self.gui_data.reset()
 
if __name__ == '__main__':
    MainApp().run()
from kivy.properties import StringProperty
from kivy.event import EventDispatcher
 
 
class Data(EventDispatcher):
    number = StringProperty("0")
    
    def reset(self):
        self.number = "0"
or how I would do it

main.py
from kivy.app import App
from kivy.config import ConfigParser
from kivy.properties import StringProperty
 
import parameters

 
class MainApp(App):
    number = StringProperty()
    def build(self):
        self.config = ConfigParser()
        self.config.read('config.ini')
        self.number = self.config.get('DEFAULT', 'value')

 
    def increment(self):
        self.number = str(int(self.number) + 1)
 
    def decrement(self):
        self.number = str(int(self.number) - 1)
 
    def reset_parameters(self):
        self.number = self.config.get('DEFAULT', 'value')
 
if __name__ == '__main__':
    MainApp().run()
main.kv
BoxLayout:
    orientation: "vertical"
    Label:
        text: app.number
 
    Button:
        text: "Increment"
        on_press: app.increment()
 
    Button:
        text: "Decrement"
        on_press: app.decrement()
 
    Button:
        text: "Reset parameters"
        on_press: app.reset_parameters()
config.ini
Output:
[DEFAULT] value = 0



RE: Kivy property (in .kv) loses binding to a variable (in .py) - j.crater - Aug-14-2018

Thanks Buran.
In the meantime I came up with exact same reset() method solution as you proposed, it works well.

I like your suggestion with Config as well. But can it be made to work as elegantly if gui_data was an object containing many attribute values? I gave an example with just one variable needed to be reset (number) just for the sake of simplicity.


RE: Kivy property (in .kv) loses binding to a variable (in .py) - buran - Aug-14-2018

Yes, you will have many options in the config file.
Also I would create separate class (for the widget) and then reference it in the kv file