首页 > 解决方案 > 如果没有选择文件,如何知道 FileChooserListView 结构下的工作目录

问题描述

我是python kivy的新手,最近我正在使用kivy来构建我的android应用程序,问题如下:

我正在使用 FileChooserListView 在我的 boxlayout 中显示所有文件系统,我知道当我选择一个或多个文件时,我可以使用 some_filechooser.path 来获取当前工作目录。

test.kivy

BoxLayout:
    size: root.size
    pos: root.pos
    orientation: "vertical"
    FileChooserListView:
        id: filechooser
        path: "./"
        on_selection: root.selected(filechooser.path)

在我触摸/单击/选择一个文件后,执行 on_selection: root.selected(filechooser.path) 以获取所选文件的当前路径,但即使没有选择文件,我也可以做同样的事情。感谢你的帮助

标签: pythonkivyworking-directory

解决方案


用于filechooser.path在未选择文件时显示当前工作目录。

片段

        BoxLayout:
            Label:
                text: 'Working Directory:'
            Label:
                text: filechooser.path

文件选择器 » 路径

小路

path 是一个StringProperty并且默认为当前工作目录作为一个 unicode 字符串。它指定该控制器应该引用的文件系统上的路径。

您可以执行以下操作:

例子

主文件

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.lang.builder import Builder

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

    def build(self):
        return Builder.load_file('main.kv')


if __name__ == '__main__':
    Editor().run()

主文件

#:kivy 1.11.0

Root:
    text_input: text_input

    BoxLayout:
        orientation: 'vertical'
        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: 'Load'
                on_release: root.show_load()
            Button:
                text: 'Save'
                on_release: root.show_save()

        BoxLayout:
            TextInput:
                id: text_input
                text: ''

            RstDocument:
                text: text_input.text
                show_errors: True

<LoadDialog>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser

        BoxLayout:
            orientation: 'vertical'
            size_hint_y: None
            height: 90

            BoxLayout:
                Label:
                    text: 'Working Directory:'
                Label:
                    text: filechooser.path

            BoxLayout:
                Label:
                    text: 'Filename:'
                Label:
                    text: ''.join(map(str, filechooser.selection))

            BoxLayout:
                Button:
                    text: "Cancel"
                    on_release: root.cancel()

                Button:
                    text: "Load"
                    on_release: root.load(filechooser.path, filechooser.selection)

<SaveDialog>:
    text_input: text_input
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser
            on_selection: text_input.text = self.selection and self.selection[0] or ''

        TextInput:
            id: text_input
            size_hint_y: None
            height: 30
            multiline: False

        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: "Cancel"
                on_release: root.cancel()

            Button:
                text: "Save"
                on_release: root.save(filechooser.path, text_input.text)

输出

FileChooser - 路径


推荐阅读