python - 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()
解决方案
您只是缺少设置属性CheckBox
state
何时被更改的代码:cb_state
GridTile
RecycleView
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
推荐阅读
- image - 张量流中的分层图像分类
- python - powershell stderr 重定向每隔几个字符就换行
- rust - 无法在“FnMut”闭包中移出捕获的变量
- arrays - 如何在 C++ 中创建一个添加多个数组的函数
- php - 如何在控制器功能中使用表单输入来输入验证码以保护 CodeIgniter 中的表单提交(不是验证而是验证)
- java - 使用 Streams 的 JsonArray 循环
- r - 避免ggplot中的代码重复:添加各种geom_sf图
- visual-studio-2015 - VS 2015 SSIS项目不断突破
- android - 定义路线之间的所有区域纬度和经度
- ruby - 你可以使用 Ruby 的块简写来调用像数组访问器这样的方法吗?