I've got 2 screens, managed by a screenmanager.
I'm using a global variable, CHOSEN_ITEM
, to save a string variable which is changed by the First Screen.
This CHOSEN_ITEM
is displayed by the Second Screen. I know StringProperty must be used but I don't find a good example, understandable by myself, for this...
from kivy.properties import ObjectProperty, StringProperty
...
CHOSEN_ITEM = ''
class FirstScreen(Screen):
...
def save_chosen(self):
global CHOSEN_ITEM
CHOSEN_ITEM = chosen_item
...
class SecondScreen(Screen):
...
global CHOSEN_ITEM
chosen_item = StringProperty(CHOSEN_ITEM)
def on_modif_chosenitem(self):
print('Chosen Item was modified')
self.bind(chosen_item=self.on_modif_chosenitem)
...
The error is then :
File "_event.pyx", line 255, in kivy._event.EventDispatcher.bind (/tmp/pip-build-udl9oi/kivy/kivy/_event.c:3738)
KeyError: 'chosen_item'
I don't get how to use bind
with StringProperty
.
Okay, I've found a solution inspired by @inclement in: Kivy ObjectProperty to update label text
from kivy.event import EventDispatcher
from kivy.properties import StringProperty
...
CHOSEN_ITEM = ''
class Chosen_Item(EventDispatcher):
chosen = StringProperty('')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.chosen = 'Default String'
self.bind(chosen=self.on_modified)
def set_chosen_item(self, chosen_string):
self.chosen = chosen_string
def get_chosen_item(self):
return self.chosen
def on_modified(self, instance, value):
print("Chosen item in ", instance, " was modified to :",value)
class FirstScreen(Screen):
global CHOSEN_ITEM
CHOSEN_ITEM = Chosen_Item()
...
def save_chosen(self):
CHOSEN_ITEM.set_chosen_item(chosen_item)
...
class SecondScreen(Screen):
...
global CHOSEN_ITEM
chosen_item = CHOSEN_ITEM.get_chosen_item()
...
It wasn't easy... for me