使用FileChooser选择文件夹

时间:2018-08-02 10:25:30

标签: python kivy filechooser

我想将Kivy Filechooser添加到gridlayout中 https://kivy.org/docs/api-kivy.uix.filechooser.html

我有我的主要班级:

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

我想将filechooser中的编辑器类添加到mylayout BoxLayout中 如果我添加

mylayout.add_widget(Editor.run())

我在全屏窗口中有文件选择器,而不是boxlayout

我希望该用户可以选择文件夹(而非文件)。

2 个答案:

答案 0 :(得分:0)

解决方案

dirselect: True添加到FileChooserListView / FileChooserIconView

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

FileChooser Controller » dirselect

dirselect
     

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

     

dirselect是一个BooleanProperty,默认值为False。

答案 1 :(得分:0)

我想我已经找到了解决方法,但是弹出的窗口是空的:

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()

enter image description here

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