Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Kivy] Kivy property (in .kv) loses binding to a variable (in .py)
#1
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
Quote
#2
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
Quote
#3
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.
Quote
#4
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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] Setting Binding to Entry created with a loop? p_hobbs 1 141 Nov-25-2019, 10:29 AM
Last Post: Larz60+
  Binding functions in Qt Designerr Mocap 12 702 Aug-22-2019, 03:38 PM
Last Post: Denni
  Binding functions to Menus in tkinter?? Mocap 1 273 Jul-23-2019, 01:37 AM
Last Post: Larz60+
  [Tkinter] Resetting scale value back to 0 after the slider loses focus lwhistler 3 2,625 Mar-07-2017, 10:01 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)