首页 > 解决方案 > Kivy 使用 RecycleView 的 CheckBox 问题

问题描述

最少的代码是用于一个图像网格,每个图像都有一个使用 Kivy、RecycleView 和 RecycleGridLayout 的复选框。问题包括: i) 复选框的选择/取消选择不显示;2) 它正在重置复选框,因此取消选择似乎是一个选择 - 请参阅 on_checkbox_active 和 on_checkbox_press 中的 print() 语句的输出(包括两者的代码)。

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.recycleview import RecycleView
from kivy.properties import StringProperty, ListProperty, NumericProperty

Builder.load_string("""
<GridTile>:
    AsyncImage:
        source: root.tile
        size_hint_y: None
        height: '150dp'
        CheckBox:
            id: ck
            root_ref: root.index  # create reference to containing GridTile
            # on_active: app.on_checkbox_active(self, self.active, self.state, self.root_ref)
            on_press: app.on_checkbox_press(self, self.active, self.state, self.root_ref)

<GridScreen>:
    name: 'grid_screen'
    RV:
        id: rv
        viewclass: 'GridTile'
        RecycleGridLayout:
            cols: 2
            size_hint_y: None
            default_size: 1, dp(150)
            default_size_hint: 1, None
            height: self.minimum_height
""")

class GridTile(Screen):
    # properties to be set in the rv.data
    tile = StringProperty('')
    index = NumericProperty(-1)
    cb_state = StringProperty('normal')

class GridScreen(Screen):
    pass

class RV(RecycleView):
    data = ListProperty('[]')

    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.cell_data()

    def cell_data(self):
        self.data = [{"tile": 'The Rolling Stones', "index": i, "cb_state": 'normal'} for i in range(41)]

class ThisApp(App):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def build(self):
        self.sm = ScreenManager()
        self.sm.add_widget(GridScreen(name='grid_screen'))
        return self.sm

    def on_checkbox_active(self, checkbox, active, state, root_ref):
        if active:
            print(active, state, root_ref)
        else:
            print(active, state, root_ref)

        new_state = checkbox.state

        # set checkbox state back to the default
        checkbox.state = 'normal'  # avoids setting checkbox state without data

        rv = self.root.get_screen('grid_screen').ids.rv
        rv.data[root_ref]['cb_state'] = new_state
        rv.refresh_from_data()  # set the state from data

    def on_checkbox_press(self, checkbox, active, state, root_ref):
        if active:
            print(active, state, root_ref)
        else:
            print(active, state, root_ref)

        new_state = checkbox.state

        # set checkbox state back to the default
        checkbox.state = 'normal'  # avoids setting checkbox state without data

        rv = self.root.get_screen('grid_screen').ids.rv
        rv.data[root_ref]['cb_state'] = new_state
        rv.refresh_from_data()  # set the state from data

if __name__ == "__main__":
    ThisApp().run()

标签: pythoncheckboxkivyrecycleview

解决方案


您只是缺少设置属性CheckBox state何时被更改的代码:cb_stateGridTileRecycleView

class GridTile(Screen):
    # properties to be set in the rv.data
    tile = StringProperty('')
    index = NumericProperty(-1)
    cb_state = StringProperty('normal')

    # code to set CheckBox state based on cb_state property
    # this could also be accomplished with a `bind`
    def on_cb_state(self, grid_tile, new_state):
        self.ids.ck.state = new_state

推荐阅读