Kivy: an alternative to legacy features

I am trying to adapt this code , but I am still the first step as I do not understand most of the functions called as SelectableDataItem , Adapter , ListAdapter or SelectableView .

When I looked at them on the kivy website, I saw that they are listed as deprecated. I have not found an alternative to these features on the Kivy website and I do not want to create an application with legacy features.

So my question is, what is the alternative to these four functions or other terms, how can I change the code so that it doesn't call deprecated functions.

+1


source to share


2 answers


Take a look at RecycleView
I wrote an example of what you might need with RecycleView



from kivy.app import App
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.uix.recycleview.views import RecycleDataViewBehavior



items = [
    {"text": "white",    "selected": 'normal', "input_data": ["some","random","data"]},
    {"text": "lightblue","selected": 'normal', "input_data": [1,6,3]},
    {"text": "blue",     "selected": 'normal', "input_data": [64,16,9]},
    {"text": "gray",     "selected": 'normal', "input_data": [8766,13,6]},
    {"text": "orange",   "selected": 'normal', "input_data": [9,4,6]},
    {"text": "yellow",   "selected": 'normal', "input_data": [852,958,123]},
    {"text": "white",    "selected": 'normal', "input_data": ["some","random","data"]},
    {"text": "lightblue","selected": 'normal', "input_data": [1,6,3]},
    {"text": "blue",     "selected": 'normal', "input_data": [64,16,9]},
    {"text": "gray",     "selected": 'normal', "input_data": [8766,13,6]},
    {"text": "orange",   "selected": 'normal', "input_data": [9,4,6]},
    {"text": "yellow",   "selected": 'normal', "input_data": [852,958,123]}
]



class MyViewClass(RecycleDataViewBehavior, BoxLayout):

    text = StringProperty("")
    index = None

    def set_state(self,state,app):
        app.root.ids.rv.data[self.index]['selected'] = state

    def refresh_view_attrs(self, rv, index, data):
        self.index = index
        return super(MyViewClass, self).refresh_view_attrs(rv, index, data)



class MyRecycleView(RecycleView):

    data = items

    def print_data(self,data):
        print([item['input_data'] for item in data if item['selected'] == 'down'])



KV = '''

<MyViewClass>:
    orientation: 'horizontal'
    CheckBox:
        on_state: root.set_state(self.state,app)
    Label:
        text: root.text

BoxLayout:
    orientation: 'vertical'
    MyRecycleView:
        id: rv
        viewclass: 'MyViewClass'
        RecycleBoxLayout:
            orientation: 'vertical'
            default_size: None, dp(56)
            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height
    Button:
        size_hint_y: 0.1
        text: "Print data"
        on_release: rv.print_data(rv.data)

'''



class Test(App):
    def build(self):
        root = Builder.load_string(KV)
        return root


Test().run()

      

+3


source


I cannot comment, so as follows:

When using a lot of text, keep in mind rstDocument, which has a lot more options. Most of the online Python documentation is based on limited text. And just in case you decide to go that route, use the retext editor for the first one.



from kivy.app import App
from kivy.uix.treeview import TreeView
from kivy.uix.treeview import TreeViewLabel
from kivy.uix.treeview import TreeViewNode
from kivy.uix.scrollview import ScrollView
from kivy.uix.rst import RstDocument
from kivy.core.window import Window
from kivy.uix.stacklayout import StackLayout
import kivy


class TreeViewRst (RstDocument,TreeViewNode):
        pass

def update_size (event1,event2):
    event1.size = event1.viewport_size

txt ="""

Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Mauris accumsan consequat justo, non aliquet sapien bibendum non. 
Nam quam risus, tempus quis lorem eu, sodales facilisis justo. 

Curabitur quis placerat ante. Phasellus tempor, libero vitae 
commodo accumsan, lorem metus eleifend velit, id dapibus ipsum ante sed massa. 
onec aliquam pellentesque pharetra. Praesent quis augue id elit facilisis euismod 
vel nec erat. Vestibulum nec dolor eget sem ullamcorper tempor id et dui. In a egestas massa.
Nulla vel augue non nisi varius varius.

"""

class TreeApp (App):
    def build (self):
        root = StackLayout()
        scroll = ScrollView(pos = (0, 0),size_hint=(1,0.78))
        body = TreeView(hide_root=True,indent_level=0,size_hint=(1,None))
        body.bind(minimum_height=body.setter('height'))
        intro = body.add_node(TreeViewLabel(text="Title",font_size=18))
        intro_diag = body.add_node(TreeViewLabel(text="Article"))
        long_txt = txt*10
        test = body.add_node(TreeViewRst(text=long_txt, size=(100,400)),parent=intro_diag)

        #test.bind(on_scroll_start=update_size)
        scroll.add_widget(body)
        root.add_widget(scroll)
        print(kivy.__version__)
        return root

Window.size = (360,640)
tree = TreeApp()
tree.run()

      

+1


source







All Articles