如何使用 Kivy 和 Python 解决文件选择器应用程序的 UnicodeDecodeError?

How to resolve UnicodeDecodeError for file selector app with Kivy and Python?

我是 kivy 的新手,正在使用 kivy 和 python 制作文件夹上传器。

我尝试使用从 this link 中找到的以下代码制作文件上传器。我想把这个文件上传器变成一个文件夹上传器,这样我就可以 select 整个目录。然后,我将对该文件夹中的项目和 return 内容执行操作。

我如何创建文件夹 select 或者 return selected 文件夹的目录?

此外,当我 运行 此代码时,我在尝试加载图像时收到以下错误。我查看了 ,并对代码进行了适当的编辑,但我仍然收到此错误。

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

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


Factory.register('Root', cls=Root)
Factory.register('LoadDialog', cls=LoadDialog)
Factory.register('SaveDialog', cls=SaveDialog)


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

编辑

我附上了editor.kv文件的代码,不过也可以在上面的link上找到。

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:
        size_hint_y: None
        height: 30
        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)

"Python tries to convert a byte-array (a bytes which it assumes to be a utf-8-encoded string) to a unicode string (str). This process of course is a decoding according to utf-8 rules. When it tries this, it encounters a byte sequence which is not allowed in utf-8-encoded strings (namely this 0xff at position 0)."

来源: