kivy FileChooserListView on_selection事件没有按预期工作



我试图通过将on_selection绑定到更新文本标签的方法来观察FileChooserListView对象的selection属性(ObservableList)。

根据我对kivy文档的解释,我认为下面的代码可以工作,但是无论如何单击或双击文件名都不会导致标签得到更新或打印语句得到执行。我是否误解了关于on_<property>变化事件的文件?

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.filechooser import FileChooserListView

class FCApp(App):
def build(self):
my_layout = AppLayout()
return my_layout

class AppLayout(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation = 'vertical'
self.lbl = Label(size_hint_y=0.1, text='Select a file...')
self.fc = FileChooserListView(size_hint_y=0.9)
# Bind changes to the file chooser's selection property to a function
self.fc.bind(on_selection=self.update_label)
self.add_widget(self.lbl)
self.add_widget(self.fc)
def update_label(self, obj):
print('update_label_called')
self.lbl.text = str(obj.selection)

if __name__ == '__main__':
FCApp().run()

如果扩展FileChooserListView,则可以使用on_<property>。在这种情况下,您可以像文档中描述的那样定义方法on_selection()

或者您可以直接使用绑定中的Property名称:

self.fc.bind(selection=self.update_label)

假设您只是更改了绑定代码,您将需要修改您的update_label()方法:

def update_label(self, fc_instance, selection):
print('update_label_called')
self.lbl.text = str(selection)

最新更新