Kivy:替代已弃用的功能

时间:2017-04-22 16:16:26

标签: python-3.x kivy

我正在尝试调整此code,但我仍然是第一步,因为我不理解大多数称为SelectableDataItemAdapterListAdapter的功能或SelectableView

当我在kivy网站上查看它们时,我看到它们被列为已弃用。我在Kivy网站上找不到这些功能的替代品,我不想构建一个具有弃用功能的应用程序。

所以我的问题是:这四个功能的替代方法是什么,或者换句话说,我应该如何修改代码,以便它不会调用已弃用的功能。

2 个答案:

答案 0 :(得分:3)

看看RecycleView
我用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()

答案 1 :(得分:1)

我无法评论,因此这样:

当您使用大量文本时,请记住rstDocument,这有更多可能性。大多数Python在线文档都基于restrctured Text。如果您决定采用这种方式,请首先使用retext Editor。

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