当前位置: 首页 > 后端技术 > Python

Kivy文件选择对话框支持中文

时间:2023-03-26 13:28:43 Python

这个问题其实和Kivy中其他支持中文的控件是一样的。是字体问题,不是字符集问题,但是我在网上能找到答案,都是围绕着字符集兜兜转转,有的很认真的回复:Addingthefile_encodings:["utf-8"]attribute给FileChooserListView就可以解决问题。其实一点用都没有——官方文档已经写好了,默认的字符集是:['utf-8','latin1','cp1252'],已经包含了utf-8的支持。实际上,只需增加font_name设置即可。比如官网例子可以改成如下模式::BoxLayout:size:root.sizepos:root.posorientation:"vertical"FileChooserListView:id:filechooser#这里设置的字体是Android标准中文font_name:'DroidSansFallback'#----------------------BoxLayout:size_hint_y:Noneheight:30Button:text:"Cancel"on_release:root.cancel()Button:text:"Load"on_release:root.load(filechooser.path,filechooser.selection)不要忘记将字体文件放入项目中。