Python Forum
[Kivy] GUI properties defined (and changed) by variables in .py file
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Kivy] GUI properties defined (and changed) by variables in .py file
#1
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
Reply
#2
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
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, 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 :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Menus and icons changed after upgrade blackclover 0 1,904 May-11-2018, 08:14 PM
Last Post: blackclover
  PyGtk3, How to Create “title-changed” signal for Gtk.Window Widget? harun2525 2 6,037 May-01-2017, 07:59 AM
Last Post: harun2525

Forum Jump:

User Panel Messages

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