如何使文件选择器成为猕猴桃,以支持不同语言(英语,希伯来语)的文件

时间:2019-01-29 16:24:07

标签: python kivy kivy-language

我正在使用kivy的文件选择器,当有希伯来语名称的文件夹文件时,它会打印乱码... 如果可能,我想支持其他语言。 试图在文件选择器中更改字体名称,对我不起作用。 您能帮我找出我在做什么错吗?

2 个答案:

答案 0 :(得分:2)

不仅FileChooser-Kivy中Label的所有实例都默认使用Roboto字体,它似乎不支持Unicode字符。尝试运行以下代码:

from kivy.app import App
from kivy.uix.label import Label


class TestApp(App):
    def build(self):
        return Label(text="עִבְרִית‎")


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

Kivy附带了several fonts,其中一个是DejaVuSans。让我们使用它:

from kivy.app import App
from kivy.uix.label import Label


class TestApp(App):
    def build(self):
        return Label(text="עִבְרִית‎", font_name='DejaVuSans.ttf')


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

现在希伯来语正确显示。但是,它不适用于日语。对于该语言,您必须寻找另一种Unicode字体,将其放置在目录中并传递到font_name属性。

无论如何。如何使FileChooser使用其他字体?最简单的方法是将方法绑定到on_entry_added事件,以更改目录树中新创建的项目的属性:

from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout


Builder.load_string("""
<MyWidget>:
    FileChooserListView
        id: filechooser
""")

class MyWidget(BoxLayout):
    def __init__(self, *args):
        Clock.schedule_once(self.update_filechooser_font, 0)
        return super().__init__(*args)

    def update_filechooser_font(self, *args):
        fc = self.ids['filechooser']
        fc.bind(on_entry_added=self.update_file_list_entry)
        fc.bind(on_subentry_to_entry=self.update_file_list_entry)    

    def update_file_list_entry(self, file_chooser, file_list_entry, *args):
        file_list_entry.ids['filename'].font_name = 'DejaVuSans.ttf'


class MyApp(App):
    def build(self):
        return MyWidget()


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

答案 1 :(得分:0)

这是我的解决方案。一半的信用归功于Nykakin:

from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout


Builder.load_string("""
<MyWidget>:
    FileChooserListView
        id: filechooser
""")

class MyWidget(BoxLayout):
    def __init__(self, *args):
        Clock.schedule_once(self.update_filechooser_font, 0)
        return super(MyWidget, self).__init__(*args)

    def update_filechooser_font(self, *args):
        fc = self.ids.filechooser
        fc.bind(on_entry_added=self.update_file_list_entry)
        fc.bind(on_subentry_to_entry=self.update_file_list_entry)    


    def update_file_list_entry(self, file_chooser, file_list_entry, *args):
        file_list_entry.ids.filename.font_name = 'DejaVuSans.ttf'

        updated_text = []
        # to count where to insert the english letters
        english_counter = 0

        # the next statements used to split the name to name, extention
        splitted = file_list_entry.ids.filename.text.split('.')
        extention = ''
        if len(splitted) > 2:
            name = '.'.join(splitted)
        elif len(splitted) == 2:
            name = splitted[0]
            extention = splitted[1]
        else:
            name = '.'.join(splitted)

        # for each char in the reversed name (extention is always English and need to not be reversed)
        for char in name[::-1]:
            # if its in Hebrew append it regularly (reversed) and make sure to zero the counter
            if u"\u0590" <= char <= u"\u05EA":
                updated_text.append(char)
                english_counter = 0
            # if its an English character append it before the last english word (to un-reverse it) and increase the counter
            else:
                updated_text.insert(len(updated_text) - english_counter, char)
                english_counter += 1

        # add the extention in the end if exists
        if extention == '':
            file_list_entry.ids.filename.text = ''.join(updated_text)
        else:
            file_list_entry.ids.filename.text = ''.join(updated_text) + '.' + extention


class MyApp(App):
    def build(self):
        return MyWidget()


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