kivy显示中文问题

第一种方法:未成功

 

# !/usr/bin/python
# -*-coding:utf-8-*-

from kivy.core.text import Label
from kivy.uix.button import Button
from kivy.app import App
#Label.register("DroidSansFallbackFull","/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf")
Label.register("wqy-microhei","/usr/share/fonts/truetype/wqy/wqy-microhei.ttc")
class MyApp(App):
    def build(self):
        return Button(text=u'看看')#未成功
if __name__=='__main__':
    MyApp().run()


第二种方法:成功

 

 

 

# !/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.core.window import Window

kivy.resources.resource_add_path("/usr/share/fonts/truetype/droid/")
font1=kivy.resources.resource_find("DroidSansFallbackFull.ttf")

class MyApp(App):
    def build(self):
        Window.fullscreen=1
        #self.title="中文标题"#不能设置font_name
        label1=Label(text="你好",font_name=font1)
        button1=Button(text="我是aphero",font_name=font1)
        return button1

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

 

上面的代码可精简:

 

 

# !/usr/bin/python
# -*-coding:utf-8-*-


from kivy.app import App
from kivy.uix.button import Button

class MyApp(App):
    def build(self):
        button1=Button(text="我是中文啊!",font_name="/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf")
        return button1

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

在kv中设置中文

from kivy.app import App
from kivy.lang import Builder

root = Builder.load_string(
'''
FloatLayout:
    canvas.before:
        Color:
            rgba:0, 1, 0, 1
        Rectangle:
            pos: self.pos
            size: self.size
    Button:
        text: '点击'
        font_name: 'DroidSansFallbackFull'
        size_hint: .5, .5
        pos_hint:{'center_x':.5, 'center_y':.5}
'''
)

class MainApp(App):
    def build(self):
        return root

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

 

 

----------------------------------------------------------------------------------------------------------------------
下面是转载部分:

 

 

来自: huangjb 2013-07-22 16:31:50

小米下将font_name指定成 /system/fonts/DroidSansFallback.ttf 可以正确显示中文
但肯定不是正确的解决方案。

kivy.core.text.__init__.py中,有这么一段话。
{{{
Label.register('DroidSans',
'data/fonts/DroidSans.ttf',
'data/fonts/DroidSans-Italic.ttf',
'data/fonts/DroidSans-Bold.ttf',
'data/fonts/DroidSans-BoldItalic.ttf')
}}}

能不能修改一下,把fallback.ttf也加进去?

 

 

 

 

你可能感兴趣的:(kivy)