【发布时间】:2021-03-02 01:45:46
【问题描述】:
我正在尝试通过将 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()
【问题讨论】: