如何格式化Kivy Listview中的滚动条

时间:2016-03-30 20:49:50

标签: python listview scrollbar kivy

我有一个ListView,其中包含listitems的自定义按钮。如何格式化滚动条以使其始终可见(如果有滚动的内容......),使其更宽(10分而不是默认值2)并赋予它不同的颜色?

DoScroll.py文件:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.listview import ListItemButton

class ShowItems(BoxLayout):
    def get_list(self):
        self.results.item_strings = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}

    def show_capital(self, capital):
        print capital

class CapitalButton(ListItemButton):
    pass

class DoScrollApp(App):
    pass

if __name__ == '__main__':
    DoScrollApp().run()

这是DoScroll.kv文件。

#: import DoScroll DoScroll
#: import ListAdapter kivy.adapters.listadapter.ListAdapter


<CapitalButton>:
    text_size: self.width - 50, None
    halign: 'left'
    valign: 'middle'
    font_size: 16
    on_press: app.root.show_capital(self.text)

ShowItems:

<ShowItems>:
    results: results_list
    orientation: "vertical"
    BoxLayout:
        height: "40dp"
        size_hint_y: None
        Button:
            text: "Get the list"
            on_press: root.get_list()
    ListView:
        id: results_list
        adapter:
            ListAdapter(data=[], args_converter=lambda row_index, 
                an_obj: {'text': an_obj,'size_hint_y': None,'height': 40}, cls=DoScroll.CapitalButton)

1 个答案:

答案 0 :(得分:2)

ListView包含ScrollView,后者持有GridView持有列表项。相应的kv规则就是这些规则(来自listview.py):

<ListView>:
    container: container
    ScrollView:
        pos: root.pos
        on_scroll_y: root._scroll(args[1])
        do_scroll_x: False
        GridLayout:
            cols: 1
            id: container
            size_hint_y: None

ScrollView包含属性bar_widthbar_colorbar_inactive_color。对于lv.container.parent.bar_width,可以通过lv = ListView()等访问这些内容。