Python Forum
[Kivy] Kivy property (in .kv) loses binding to a variable (in .py)
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
Reply
#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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#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.
Reply
#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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Key Binding scope angus1964 1 1,171 Jun-30-2022, 08:17 PM
Last Post: deanhystad
  kivy binding issue hammer 8 2,943 Nov-07-2021, 11:34 PM
Last Post: hammer
  [Tkinter] binding versus disable DPaul 6 6,602 May-05-2021, 05:17 PM
Last Post: DPaul
  [Tkinter] Change property of custom Object Sancho_Pansa 0 1,408 Dec-17-2020, 10:53 AM
Last Post: Sancho_Pansa
  [Tkinter] Binding Entry box to <Button-3> created in for loop iconit 5 4,879 Apr-22-2020, 05:47 AM
Last Post: iconit
  TkInter Binding Buttons ifigazsi 5 4,172 Apr-06-2020, 08:30 AM
Last Post: ifigazsi
  Making text clickable with binding DT2000 10 5,035 Apr-02-2020, 10:11 PM
Last Post: DT2000
  [Tkinter] Setting Binding to Entry created with a loop? p_hobbs 1 2,042 Nov-25-2019, 10:29 AM
Last Post: Larz60+
  Binding functions in Qt Designerr Mocap 12 5,788 Aug-22-2019, 03:38 PM
Last Post: Denni
  Binding functions to Menus in tkinter?? Mocap 1 2,426 Jul-23-2019, 01:37 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020