Skip to content Skip to sidebar Skip to footer

Kivy: Alternative To Deprecated Features

I am trying adapt this code but I am still the first step as I don't understand most of the features called like like SelectableDataItem, Adapter, ListAdapter or SelectableView. W

Solution 1:

Take a look at RecycleView I wrote an example of what you might want, 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]}
]



classMyViewClass(RecycleDataViewBehavior, BoxLayout):

    text = StringProperty("")
    index = Nonedefset_state(self,state,app):
        app.root.ids.rv.data[self.index]['selected'] = state

    defrefresh_view_attrs(self, rv, index, data):
        self.index = index
        returnsuper(MyViewClass, self).refresh_view_attrs(rv, index, data)



classMyRecycleView(RecycleView):

    data = items

    defprint_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)

'''classTest(App):
    defbuild(self):
        root = Builder.load_string(KV)
        return root


Test().run()

Solution 2:

I can't comment, therefore this way:

When you are using much text, have rstDocument in mind, which has much more possibilities. Most of Python online documentation is based on restrctured Text. And just in case you decide to go this way, use the retext Editor for rst.

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


classTreeViewRst (RstDocument,TreeViewNode):
        passdefupdate_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.

"""classTreeApp (App):
    defbuild (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()

Post a Comment for "Kivy: Alternative To Deprecated Features"