python - 更改 kivy.label 的 selected 属性
问题描述
我正在使用 kivy 示例代码,这是我的问题。
第 60、61 行:我想在触摸标签时显示消息。
第 62 行:所以,我更改了它的 selected 属性,我可以再次触摸标签并显示消息而无需取消其 selected 属性。
但是,不工作。我该如何解决这个错误,知道吗?
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.label import Label
from kivy.properties import BooleanProperty
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
Builder.load_string('''
<SelectableLabel>:
# Draw a background to indicate selection
canvas.before:
Color:
rgba: (0, 0, 0, 1)
Rectangle:
pos: self.pos
size: self.size
<RV>:
viewclass: 'SelectableLabel'
SelectableRecycleBoxLayout:
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
multiselect: True
touch_multiselect: True
''')
class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
RecycleBoxLayout):
''' Adds selection and focus behaviour to the view. '''
class SelectableLabel(RecycleDataViewBehavior, Label):
''' Add selection support to the Label '''
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
def refresh_view_attrs(self, rv, index, data):
''' Catch and handle the view changes '''
self.index = index
return super(SelectableLabel, self).refresh_view_attrs(
rv, index, data)
def on_touch_down(self, touch):
''' Add selection on touch down '''
if super(SelectableLabel, self).on_touch_down(touch):
return True
if self.collide_point(*touch.pos) and self.selectable:
return self.parent.select_with_touch(self.index, touch)
def apply_selection(self, rv, index, is_selected):
self.selected = is_selected
if is_selected:
print(index)
print("selection changed to {0}".format(rv.data[index]))
self.selected = not is_selected
class RV(RecycleView):
def __init__(self, **kwargs):
super(RV, self).__init__(**kwargs)
self.data = [{'text': str(x)} for x in range(100)]
class TestApp(App):
def build(self):
return RV()
if __name__ == '__main__':
TestApp().run()
解决方案
更改 .kv 文件的多选 >.<
推荐阅读
- javascript - jQuery使用foreach函数选择所有输入
- javascript - OutlinedInput labelWidth 百分比@material-ui
- jquery - 我如何获得比 datepicker 中选择的日期多 15 天的日期?
- python - 函数创建按钮但它们不能触发其他函数
- android - 在状态栏中添加按钮
- python - 如何制作高通滤波器?
- java - 如何制作一个 JList,其中每个 Item 包含 JCheckBox 和 JLabel,点击时具有不同的事件
- windows - PHP 警告:PHP 启动:加载失败,系统找不到指定的文件。PHP 7.4 视窗
- excel - 在 Access 中运行 Excel VBA
- r - 多组的密度图显示一条线,但图例显示 3