【问题标题】:kivy FileChooserListView on_selection event not working as expectedkivy FileChooserListView on_selection 事件未按预期工作
【发布时间】: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()

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    如果您扩展FileChooserListView,则可以使用on_<property>。在这种情况下,您可以定义一个方法on_selection(),如documentation 中所述。

    或者您可以在绑定中使用 Property 名称:

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

    here所述。

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

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

    【讨论】:

    • 现在我可以获取选定文件的列表,是否有希望将项目表示为可视化? (我在其他 Stack Overflow 和 kivy GitHub 存储库问题中读到多选是 FileChooserListView 和底层 TreeView 的问题)。如果您对此问题的解决方法有任何经验,我将非常感谢您的意见。
    • 我的解决方法是使用FileChooserIconView
    • 对我也不起作用...有什么建议吗?只需双击触发 on_submit ...
    猜你喜欢
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多