kivy中文的支持

kivy支持中文的方式 

1、让每个组件指定中文字体(font_name)

  • 通过kivy.resources.resource_find 查找字体
  • 通过Label或者LabelBase等注册字体
# !/usr/bin/python
# -*-coding:utf-8-*-

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
from kivy.core.text import LabelBase
from kivy.config import Config

Config.write()


# # 加载字体资源
kivy.resources.resource_add_path("./container_kvs")
font1 = kivy.resources.resource_find("msgothic.ttc")
#
# #通过labelBase
LabelBase.register("msgothic_labelBase","msgothic2.ttc")
kivy.core.text.Label.register("msgothic_label","msgothic2.ttc")



class MyApp(App):

    def build(self):
        Window.fullscreen = 1
        layout = GridLayout(cols=2,spacing=2);
        layout.add_widget(Button(text="按钮默认字体"))
        layout.add_widget(Label(text="标签-默认字体"))
        layout.add_widget(Button(text="指定定义字体",font_name=font1))
        layout.add_widget(Label(text="指定定义字体", font_name=font1))
        layout.add_widget(Label(text="查找字体", font_name="msgothic.ttc",outline_color='#666666')) # 如果不指定后缀则默认会自动查找ttf格式的字体
        layout.add_widget(Button(text="查找字体2", font_name="msgothic2.ttc",markup=True))
        layout.add_widget(Label(text="label注册字体", font_name="msgothic_label")) # 别名指定
        layout.add_widget(Button(text="labelBase注册字体", font_name="msgothic_labelBase")) # 别名指定
        return layout

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

效果:(部分文字展示不出,是因为选择的字体的原因)

 

 

2、修改默认配置[ 对所有应用有效 ]

 #如果不将配置写在最上面,则可能第二次运行才能看到效果

from kivy.config import Config

Config.set('kivy', 'default_font', [
    'msgothic',
    'fonts/txb2.ttf'])
# Config.write() # 有此句,将会把设置更新到全局配置文件中


import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
from kivy.core.text import LabelBase
from kivy.config import Config



# # 加载字体资源
kivy.resources.resource_add_path("./container_kvs")
font1 = kivy.resources.resource_find("msgothic.ttc")
#
# #通过labelBase
LabelBase.register("msgothic_labelBase","msgothic2.ttc")
kivy.core.text.Label.register("msgothic_label","msgothic2.ttc")



class MyApp(App):

    def build(self):
        Window.fullscreen = 1
        layout = GridLayout(cols=2,spacing=2);
        layout.add_widget(Button(text="按钮默认字体"))
        layout.add_widget(Label(text="标签-默认字体"))
        layout.add_widget(Button(text="指定定义字体",font_name=font1))
        layout.add_widget(Label(text="指定定义字体", font_name=font1))
        layout.add_widget(Label(text="查找字体", font_name="msgothic.ttc",outline_color='#666666')) # 如果不指定后缀则默认会自动查找ttf格式的字体
        layout.add_widget(Button(text="查找字体2", font_name="msgothic2.ttc",markup=True))
        layout.add_widget(Label(text="label注册字体", font_name="msgothic_label")) # 别名指定
        layout.add_widget(Button(text="labelBase注册字体", font_name="msgothic_labelBase")) # 别名指定
        return layout

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

效果:

注意:如果加入Config.writer();将会影响所有kivy相关的一些应用,可能会造成部分无法运行,出现以下错误;

 OSError: File fonts/txb2.ttf not found

  • 解决方案一:重新设置默认字体

  • 解决方案二:恢复默认配置,也就是删除kivy的配置文件【config.ini】或者修改为原始的默认配置;

 

window下kivy的配置文件所在位置   

    C:\Users\当前用户\.kivy\config.ini  

字体默认配置

default_font = ['Roboto', 'data/fonts/Roboto-Regular.ttf', 'data/fonts/Roboto-Italic.ttf', 'data/fonts/Roboto-Bold.ttf', 'data/fonts/Roboto-BoldItalic.ttf']

 

猜你喜欢

转载自blog.csdn.net/xia872409653/article/details/81076131