不仅仅是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()