从Kivy Recycleview获取选定节点(Get selected node from Kivy Recycleview)
我有一些使用ListViews在以前版本的Kivy中编写的程序。 使用ListViews,通过适配器获取所选节点非常容易。 但是,如何使用RecycleView进行此操作还不太清楚。 现在,
rv.layout_manager.selected_nodes
可用于获取所选值,但也有时候我对实际节点感兴趣。 此外,以下代码段可用于生成节点,但显然,它们不是RecycleView中的实际节点。opts = rv.layout_manager.view_opts for i in range(len(rv.data)): s = rv.view_adapter.get_view(i, rv.data[i], opts[i]['viewclass']) print s.text, s.selected
我有兴趣找到一种从RecycleView获取所选节点的方法。
完整代码:
import random 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 #Aside: the code in the string would need to be indented back one tab, but it's like this for SO formatting Builder.load_string(''' <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 <RV>: viewclass: 'SelectableLabel' SelectableRecycleBoxLayout: key_selection: "True" default_size: None, dp(56) default_size_hint: 1, None size_hint_y: None height: self.minimum_height orientation: 'vertical' multiselect: False touch_multiselect: True touch_deselect_last: 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): ''' Respond to the selection of items in the view. ''' self.selected = is_selected class RV(RecycleView): def __init__(self, **kwargs): super(RV, self).__init__(**kwargs) self.data = [{'text': str(random.random())} for x in range(20)] class TestApp(App): def build(self): self.rv = RV() self.rv.layout_manager.bind(selected_nodes=self.selectionChange) return self.rv def selectionChange(self, inst, val): print inst, val if __name__ == '__main__': b = TestApp() b.run()
I've got some programs written in previous versions of Kivy that use ListViews. Using ListViews, it was quite easy to get the selected node via the adapter. However, it is much less clear how to do this with a RecycleView. Now, it is the case that
rv.layout_manager.selected_nodes
can be used to get the selected value, but there are also times where I'm interested in the actual node. Also, the following snippet can be used to generate the nodes, but apparently, they aren't the actual nodes in the RecycleView.opts = rv.layout_manager.view_opts for i in range(len(rv.data)): s = rv.view_adapter.get_view(i, rv.data[i], opts[i]['viewclass']) print s.text, s.selected
I'd be interested in finding a way of getting the selected node from the RecycleView.
Full code:
import random 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 #Aside: the code in the string would need to be indented back one tab, but it's like this for SO formatting Builder.load_string(''' <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 <RV>: viewclass: 'SelectableLabel' SelectableRecycleBoxLayout: key_selection: "True" default_size: None, dp(56) default_size_hint: 1, None size_hint_y: None height: self.minimum_height orientation: 'vertical' multiselect: False touch_multiselect: True touch_deselect_last: 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): ''' Respond to the selection of items in the view. ''' self.selected = is_selected class RV(RecycleView): def __init__(self, **kwargs): super(RV, self).__init__(**kwargs) self.data = [{'text': str(random.random())} for x in range(20)] class TestApp(App): def build(self): self.rv = RV() self.rv.layout_manager.bind(selected_nodes=self.selectionChange) return self.rv def selectionChange(self, inst, val): print inst, val if __name__ == '__main__': b = TestApp() b.run()
原文:https://stackoverflow.com/questions/46739914
最满意答案
从
[
有三个参数的bash
手册页(注意我的重点):以列出的顺序应用以下条件。
如果第二个参数是上面在CONDITIONAL EXPRESSIONS下列出的二进制条件运算符之一,则表达式的结果是使用第一个和第三个参数作为操作数的二进制测试的结果。 当有三个参数时,
'-a'
和'-o'
运算符被认为是二元运算符。如果第一个参数是
'!'
,该值是使用第二和第三个参数的双参数测试的否定。如果第一个参数完全是
'('
,第三个参数完全是')'
,那么结果就是第二个参数的单参数测试。否则,表达式是错误的。
换句话说,
-a
在这种情况下并不像你期望的那样发挥作用。 它实际上被视为逻辑和操作符,因为两者都是!
和bar
被认为是真实的,他们的结果也是如此:pax> if [ ! ] ; then echo yes ; fi yes pax> if [ bar ] ; then echo yes ; fi yes
[
各种各样的弱点,这就是为什么明智的bash
编码器会使用[[
相反:-)]From the
bash
man page for[
with three arguments (note my emphasis):The following conditions are applied in the order listed.
If the second argument is one of the binary conditional operators listed above under CONDITIONAL EXPRESSIONS, the result of the expression is the result of the binary test using the first and third arguments as operands. The
'-a'
and'-o'
operators are considered binary operators when there are three arguments.If the first argument is
'!'
, the value is the negation of the two-argument test using the second and third arguments.If the first argument is exactly
'('
and the third argument is exactly')'
, the result is the one-argument test of the second argument.Otherwise, the expression is false.
In other words,
-a
is not acting as you expect it to in this case. It's actually being treated as the logical-and operator and, since both!
andbar
are considered true, the result ofand
ing them is also true:pax> if [ ! ] ; then echo yes ; fi yes pax> if [ bar ] ; then echo yes ; fi yes
The foibles of
[
are many and varied, which is why sensiblebash
coders will use[[
instead :-)
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
你不能可靠地做到这一点。 在您检查文件是否存在以及何时创建文件时,可能会有另一个进程创建它。 你应该继续创建文件。 根据您要做的更大的事情,如果文件已经存在,您可能需要以下某个选项来执行该操作: 修改之前的内容: open("file", O_RDWR|O_CREAT, 0666) 擦除以前的内容: open("file", O_WRONLY|O_CREAT|O_TRUNC, 0666 ) 附加到以前的内容: open("file", O_WRONLY|O_CREAT|O_APPEND, 0666) 操作失 ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
您可以简单地继续进行替换,如果模式存在某些内容将被更改,如果不存在,则不会对该文件执行任何操作。 awk '{sub(/Pattern1/, "\nWORD"); print}' $FILE > tmp && mv tmp $FILE 检查这个问题 。 You could simply go ahead with the substitution, if the pattern exist something will be changed, if not, nothing will be done to ...
-
我使用模拟对象,而不是从类名创建一个新对象: public function getMockSubscriber(array $events) { $subscriberMock = $this->getMock( 'Symfony\Component\EventDispatcher\EventSubscriberInterface', array_merge(array_values($events), array('getSubscribedEvents')) ...
-
从[有三个参数的bash手册页(注意我的重点): 以列出的顺序应用以下条件。 如果第二个参数是上面在CONDITIONAL EXPRESSIONS下列出的二进制条件运算符之一,则表达式的结果是使用第一个和第三个参数作为操作数的二进制测试的结果。 当有三个参数时, '-a'和'-o'运算符被认为是二元运算符。 如果第一个参数是'!' ,该值是使用第二和第三个参数的双参数测试的否定。 如果第一个参数完全是'(' ,第三个参数完全是')' ,那么结果就是第二个参数的单参数测试。 否则,表达式是错误的。 换句话说, ...
-
你可以使用$.ajax $.ajax({ url: 'example.com/abc.html', //or your url success: function(data){ alert('exists'); }, error: function(data){ alert('does not exist'); }, }) You can use $.ajax $.ajax({ url: 'example.com/abc.html', //or your url ...
-
你可以这样做: $checkfile = if ("$configFile") { Test-Path -LiteralPath $configFile } else { $false } 双引号可防止误报,例如,如果您要测试是否存在名为0的文件夹。 另一种选择是设置$ErrorActionPreference 。 但是,在这种情况下,您需要将Test-Path的结果Test-Path转换为布尔 ...
-
检查p4 help files 。 简而言之,您运行p4 files
,它将为您提供该文件的库路径。 如果它不在仓库中,你将得到“没有这样的文件”。 Check p4 help files. In short, you run p4 files and it will give you the depot path to that file. If it isn't in the depot, you'll get "no such fil ... -
解决方案是使用file_test() 这提供了shell风格的文件测试,并且可以从文件夹中区分文件。 例如 input.good = "~/directory/file.txt" input.bad = "~/directory/" file_test("-f", input.good) # returns TRUE file_test("-f", input.bad) #returns FALSE 从手册: 用法 file_test(op,x,y)参数 op指定要执行的测试的字符串。 一元测试(仅使用 ...