python - 通过 Kivy 中的选定项目更改变量
问题描述
所以我学习了 Kivy,但我坚持使用ListView
或者,因为ListView
似乎已被弃用,RecycleView
.
我的问题是,我希望species_text
ID 为标签的标签会根据我单击的项目进行更改,一旦标签可见。
该文档帮助我制作SelectableLabel
并能够单击/着色,但我不知道如何species_text
通过数据列表更改文本RecycleView
或如何将数据列表保存在ScreenTest
类中。
这是我的代码:
from kivy.app import App
from kivy.uix.button import Label, Button
from kivy.lang import Builder
from kivy.properties import BooleanProperty
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_string("""
<ScreenTest>:
Label:
pos_hint: {"x": .45, "top": 1}
text: "Headline"
size_hint: .1, .1
BoxLayout:
pos_hint: {"x": 0.02, "top": .8}
RecycleView:
id: species_list_view
data: [{'name': "Species1", "text": "S1"}, {'name': "Species2", "text": "S2"}]
viewclass: 'SelectableLabel'
SelectableRecycleBoxLayout:
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
orientation: 'vertical'
multiselect: False
touch_multiselect: False
Label:
id: species_text
text: "Speciestext"
BoxLayout:
size_hint_y: None
height: 30
spacing: 10
canvas:
Color:
rgba: .5, .2, .1, 1
Rectangle:
pos: self.pos
size: self.size
Button:
text: "Go Back"
Button:
text: "Next"
<SelectableLabel>:
# Draw a background to indicate selection
canvas.before:
Color:
rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
Rectangle:
pos: self.pos
size: self.size
""")
class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
RecycleBoxLayout):
pass
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):
''' Respond to the selection of items in the view. '''
self.selected = is_selected
if is_selected:
print("selection changed to {0}".format(rv.data[index]))
else:
print("selection removed for {0}".format(rv.data[index]))
class ScreenTest(Screen):
pass
screen_manager = ScreenManager()
screen_manager.add_widget(ScreenTest(name="screen_test"))
class TestApp(App):
def build(self):
return screen_manager
test_app = TestApp()
test_app.run()
谢谢你的帮助!
解决方案
该apply_selection()
方法有RecycleView
参数,考虑到我们可以创建一个具有选定文本的属性,然后我们与 的文本进行绑定Label
:
...
RecycleView:
id: species_list_view
data: [{'name': "Species1", "text": "S1"}, {'name': "Species2", "text": "S2"}]
viewclass: 'SelectableLabel'
text_selected: "" # create property
SelectableRecycleBoxLayout:
...
Label:
id: species_text
text: "Speciestext" if not species_list_view.text_selected else species_list_view.text_selected # apply binding
...
def apply_selection(self, rv, index, is_selected):
''' Respond to the selection of items in the view. '''
self.selected = is_selected
if is_selected:
print("selection changed to {0}".format(rv.data[index]))
rv.text_selected = rv.data[index]['text'] # set text
else:
print("selection removed for {0}".format(rv.data[index]))
推荐阅读
- flutter - Flutter Web Firestore 错误:预期类型为“((Object?)=> Object)?”,但得到类型“(Object)=> Object?”之一
- join - PySpark 广播连接,其中左表比右表更小以适合内存
- flutter - Flutter:在关闭控制器上处理 HTTP 请求
- c - 一个字符串在一个点正确而在另一个点错误地打印它的内容,没有变化
- c# - 你如何在 C# 中制作向玩家射击的光线投射?
- c# - 在 XNA 中加载纹理的最快方法是什么
- javascript - 根据文本获取 div 元素
- go - 如何在切片中搜索元素并将其与变量进行比较
- r - 有没有办法使用 read.fortran() 来读取字符串而不是文件?
- python - 如何在我的代码中解决以下问题?