python库Kivy中文乱码问题

来源:互联网 发布:石膏板隔音墙数据 编辑:程序博客网 时间:2024/06/06 12:36

(1)以下摘自Kivy安装目录下label.py源代码的注释,可以给我们启发:

 font_name = StringProperty('Roboto')
    '''Filename of the font to use. The path can be absolute or relative.
    Relative paths are resolved by the :func:`~kivy.resources.resource_find`
    function.


    .. warning::


        Depending of your text provider, the font file can be ignored. However,
        you can mostly use this without problems.


        If the font used lacks the glyphs for the particular language/symbols
        you are using, you will see '[]' blank box characters instead of the
        actual glyphs. The solution is to use a font that has the glyphs you
        need to display. For example, to display |unicodechar|, use a font such
        as freesans.ttf that has the glyph.


        .. |unicodechar| image:: images/unicode-char.png


    :attr:`font_name` is a :class:`~kivy.properties.StringProperty` and
    defaults to 'Roboto'.

2)Font字体文件默认搜索目录:
运行kivy.resources.resource_paths,
Out[17]: 
['.',
 'D:/MyData',(python的working directory)
 'C:\\Anaconda3\\lib\\site-packages\\kivy',
 'C:\\Anaconda3\\lib\\site-packages\\kivy\\data\\..',
 'C:\\Windows\\Fonts',
 'C:\\Anaconda3\\lib\\site-packages\\kivy\\data\\fonts']
该目录可以自己修改。

3)关键步骤

修改Kivy默认字体
打开label.py文件,文件位于Anaconda3\\lib\\site-packages\\kivy目录;

在437行可看到如下语句:
font_name = StringProperty('Roboto')

把'Roboto'替换成中文字体,例如

'msyh',  表示微软雅黑msyh.ttf
'simsun.ttc',  表示宋体simsun.ttc

kivy会自动在(2)中的目录去搜索该字体文件


4)后面用到.kv文件时,这一部很关键,花了很长时间才自己探索出来解决办法

如图,在lang.py文件中添加红框中的语句,然后保存。



0 0
原创粉丝点击