【问题标题】:choose folder with FileChooser使用 FileChooser 选择文件夹
【发布时间】:2018-08-02 10:25:30
【问题描述】:

我想将 kivy 文件选择器添加到 gridlayout https://kivy.org/docs/api-kivy.uix.filechooser.html

我有我的主要课程:

class MainApp(GridLayout):
    def __init__(self, **kwargs):
        mylayout = BoxLayout(orientation='vertical')

我想将文件选择器中的编辑器类添加到 mylayout BoxLayout 如果我添加

mylayout.add_widget(Editor.run())

我的窗口全屏显示文件选择器,而不是 boxlayout

我希望用户可以选择文件夹(不是文件)。

【问题讨论】:

  • 您要添加 FileChooser 还是 Editor?
  • 我想添加“FileChooser”我希望用户可以选择文件夹(不是文件)
  • 如果你勾选Editor is not a widget but the application of the example,那就不正确了。

标签: python kivy filechooser


【解决方案1】:

解决方案

dirselect: True 添加到 FileChooserListView / FileChooserIconView

        FileChooserListView:
            id: filechooserlistview
            dirselect: True
            path:
                filechooserlistview.path

FileChooser Controller » dirselect

dirselect

确定目录是否为有效选择。

dirselect 是一个 BooleanProperty,默认为 False。

【讨论】:

    【解决方案2】:

    我想我已经找到了解决方案,但弹出窗口为空:

    from kivy.app import App
    from kivy.uix.floatlayout import FloatLayout
    from kivy.factory import Factory
    from kivy.properties import ObjectProperty
    from kivy.uix.popup import Popup
    
    import os
    
    
    class LoadDialog(FloatLayout):
        load = ObjectProperty(None)
        cancel = ObjectProperty(None)
    
    
    class SaveDialog(FloatLayout):
        save = ObjectProperty(None)
        text_input = ObjectProperty(None)
        cancel = ObjectProperty(None)
    
    
    class Root(FloatLayout):
        loadfile = ObjectProperty(None)
        savefile = ObjectProperty(None)
        text_input = ObjectProperty(None)
    
        def dismiss_popup(self):
            self._popup.dismiss()
    
        def show_load(self):
            content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
            self._popup = Popup(title="Load file", content=content,
                                size_hint=(0.9, 0.9))
            self._popup.open()
    
        def show_save(self):
            content = SaveDialog(save=self.save, cancel=self.dismiss_popup)
            self._popup = Popup(title="Save file", content=content,
                                size_hint=(0.9, 0.9))
            self._popup.open()
    
        def load(self, path, filename):
            with open(os.path.join(path, filename[0])) as stream:
                self.text_input.text = stream.read()
    
            self.dismiss_popup()
    
        def save(self, path, filename):
            with open(os.path.join(path, filename), 'w') as stream:
                stream.write(self.text_input.text)
    
            self.dismiss_popup()
    
    
    class Editor(App):
        pass
    
    import kivy
    
    from kivy.app import App
    from kivy.uix.label import Label
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.button import Button
    
    class MyApp(App):
    
        def build(self):
            layout = BoxLayout(orientation='vertical')
            root=Root()
            root.show_load()
            layout.add_widget(root)
            return layout
    
    if __name__ == '__main__':
        MyApp().run()
    

    为什么我没有弹出文件夹?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-21
      • 2012-09-20
      • 2011-09-04
      • 2011-05-21
      相关资源
      最近更新 更多