Python Forum

Full Version: GUI properties defined (and changed) by variables in .py file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all,
I am trying to make a basis for a MVC-sytle program, where GUI properties (in .kv file) would be stored and accessed from a separate .py file. I tried a couple of things, but as I am not yet too familiar with Kivy, I had no success so far.
Value of label_text does change with pressing buttons (increment or decrement), but label text remains same ("0"), though I would like it to change dynamically.
The code in files is:

main.py:

from kivy.app import App

import values


class MainApp(App):
    def increment(self):
        values.label_text = str(int(values.label_text) + 1)

    def decrement(self):
        values.label_text = str(int(values.label_text) - 1)


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


label_text = "0"
# label_text = StringProperty("0") # gives: ValueError: Label.text accept only str
main.kv:
#:import val values


BoxLayout:
    orientation: "vertical"

    Label:
        text: val.label_text

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

    Button:
        text: "decrement"
        on_press: app.decrement()
Any tips are most appreciated, thanks.
JC
main.py

from kivy.app import App
from kivy.properties import StringProperty
import values

class MainApp(App):
    label_text = StringProperty()
    def build(self):
        self.label_text = values.label_text
    def increment(self):
        self.label_text = str(int(self.label_text) + 1)
    def decrement(self):
        self.label_text = str(int(self.label_text) - 1)
 
if __name__ == '__main__':
    MainApp().run()
main.kv
Output:
BoxLayout: orientation: "vertical" Label: text: app.label_text Button: text: "increment" on_press: app.increment() Button: text: "decrement" on_press: app.decrement()
values.py
label_text = "0"
However I would use kivy.config and eventually add kivy.settings to initialize and have settings shown to user
Thanks buran, this works.
Meanwhile I played with a bit more complex "settings", such as saving properties in objects as their attributes.
Kivy ObjectProperty and EventDispatcher came in very handy here and seem to be quite powerful tools. And hopefully not too dangerous in hands of a beginner :)