I have a problem which may be quite python-ignorant rather than kivy problem.
I cant program kivy in python so I have KV file and py file.
I have modified Recycleview with SelectableLabel from kivy documentation. However, unlike all other widget, this one works differently.
This is main.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from kivy.config import Config
try:
import kivy
except ImportError:
raise ImportError("this backend requires Kivy to be installed.")
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.label import Label
from kivy.properties import BooleanProperty
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
RecycleBoxLayout):
''' Adds selection and focus behaviour to the view. '''
class SelectableLabel(RecycleDataViewBehavior, Label):
''' Add selection support to the Label '''
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
def refresh_view_attrs(self, rv, index, data):
''' Catch and handle the view changes '''
self.index = index
return super(SelectableLabel, self).refresh_view_attrs(
rv, index, data)
def on_touch_down(self, touch):
''' Add selection on touch down '''
if super(SelectableLabel, self).on_touch_down(touch):
return True
if self.collide_point(*touch.pos) and self.selectable:
return self.parent.select_with_touch(self.index, touch)
def apply_selection(self, rv, index, is_selected):
''' Respond to the selection of items in the view. '''
self.selected = is_selected
self.change_this_variable = 'Here'
"""I wish to change this variable that belongs to MainWindow instance
"""
if is_selected:
print("selection changed to {0}".format(rv.data[index]))
else:
print("selection removed for {0}".format(rv.data[index]))
class MainWindow(BoxLayout):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.change_this_variable = '' # Variable I want to change from
# select_event
#My whole app logic is here
#
#
class guitApp(App):
pass
if __name__ == '__main__':
guitApp().run()
Here is guit.kv
MainWindow:
BoxLayout:
RecycleView:
id: rv
data:[{'text': str(x)} for x in range(10)] # #
viewclass: 'SelectableLabel'
SelectableRecycleBoxLayout:
id: slr
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
multiselect: False
touch_multiselect: True
<SelectableLabel>:
# Draw a background to indicate selection
canvas.before:
Color:
rgba: (1, 0.866, .0, 1) if self.selected else (0, 0.419, 1,1)
Rectangle:
pos: self.pos
size: self.size
My problem is, that I usually create widgets in kivy language, give it id and do logic in main.py via id reference or with on_event: root.function() in kivy lang. But with this SelecteableLabel and RecycleView from documentation, I am given on_event function called apply_selection but it is outside Gui Object(MainWindow, where all logic happens) so I cant access it. I dont want to solve this with Globals. So my question is, how to get apply_selection inside my Gui object so I can assign values to MainWindow's variables (like self.change_this_variable)?
There is no need to get apply_selection() method inside MainWindow(). The solution is to use App.get_running_app().root
to get an instance of MainWindow() and you can reference its attributes and methods as follow:
def apply_selection(self, rv, index, is_selected):
''' Respond to the selection of items in the view. '''
self.selected = is_selected
App.get_running_app().root.change_this_variable = 'Here'
"""I wish to change this variable that belongs to MainWindow instance
"""
if is_selected:
print("selection changed to {0}".format(rv.data[index]))
else:
print("selection removed for {0}".format(rv.data[index]))