【问题标题】:Kivy FileChooser: List directories onlyKivy FileChooser:仅列出目录
【发布时间】:2016-03-15 20:35:26
【问题描述】:

如何在 Kivy FileChooser 中列出 only 目录?我已经阅读了有关回调过滤器的信息,但没有找到任何示例。

我的Kv代码:

<Saveto>:
    select: select
    chooser: chooser
    orientation: 'vertical'
    FileChooserIconView:
        id: chooser
        size_hint_y: 0.9
        rootpath: home
        dirselect: True
        filters: ['How to list folders only?']
    Button:
        ...select button...

【问题讨论】:

    标签: python user-interface python-3.x kivy filechooser


    【解决方案1】:

    这是一个例子:

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.lang import Builder
    
    from os.path import join, isdir
    
    Builder.load_string("""
    <MyWidget>:
        FileChooserListView:
            filters: [root.is_dir]
    """)
    
    class MyWidget(BoxLayout):
        def is_dir(self, directory, filename):
            return isdir(join(directory, filename))
    
    class MyApp(App):
        def build(self):
            return MyWidget()
    
    if __name__ == '__main__':
        MyApp().run()
    

    请注意,该属性称为filters,而不是filter,因为它是一个列表,例如回调列表。

    【讨论】:

    • 窗口立即为我打开和关闭,该解决方案是否已过时?
    • 在 Linux 中为我工作。控制台日志中有任何错误吗?
    • 只是“进程完成,退出代码 -1073741819 (0xC0000005)”,日志中没有任何用处。使用 Windows 10 64 位
    • 不……在日志中没有得到任何错误输出,只是一个退出代码。日志显示为正常的应用程序,没有发生任何错误
    【解决方案2】:

    你也可以直接在KV里做:

    <Saveto>:
        select: select
        chooser: chooser
        orientation: 'vertical'
        FileChooserIconView:
            id: chooser
            size_hint_y: 0.9
            rootpath: home
            dirselect: True
            filters: [lambda folder, filename: not filename.endswith('')]
        Button:
            ...select button...
    

    【讨论】:

      【解决方案3】:

      如果你想使用没有任何文件的目录,你可以使用:

      os.path.dirname(filename)
      

      以如下方式(目录仅用于将目录打印为屏幕右侧的标签的示例):

      from kivy.app import App
      from kivy.uix.boxlayout import BoxLayout
      from kivy.uix.label import Label
      from kivy.lang import Builder
      import os
      
      
      Builder.load_string("""
      <MyWidget>:
          FileChooserIconView:
              filters: [root.selected]
          BoxLayout:
              Label:
                  id: mypath
                  text: ''
      """)
      
      class MyWidget(BoxLayout):
          def selected(self, directory, filename):
              self.ids.mypath.text = os.path.dirname(filename)
      
      
      class MyApp(App):
          def build(self):
              return MyWidget()
      
      if __name__ == '__main__':
          MyApp().run()
      

      希望对你有帮助

      【讨论】:

        【解决方案4】:

        这有点小技巧,但似乎可以工作并且只能在 kv 文件中完成:

        dirselect: True
        filters: ['']
        

        FileChooser 中仅显示文件夹。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-01
          • 1970-01-01
          • 2019-06-03
          • 1970-01-01
          • 1970-01-01
          • 2017-07-19
          相关资源
          最近更新 更多