在kivy列表视图项中写波斯语

时间:2014-12-23 08:29:17

标签: android python listview kivy persian

我在kivy编程中遇到了另一个问题。 我想在我的应用程序和使用过的Arabi_reshaper中编写波斯语。 当我试着这样做时:

# -*- coding: utf-8 -*-
import kivy
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from bidi.algorithm import get_display
import arabic_reshaper
Builder.load_string(
'''
<TI>
    but: but
    Button:
        id: but
        font_name: 'data/fonts/DejaVuSans.ttf'
        font_size: '45dp'   
''')

class TI(FloatLayout):

    def __init__(self, **kwargs):
        super(TI, self).__init__(**kwargs)
        self.but.text = get_display(arabic_reshaper.reshape(u'سلام دنیا'))


class MyApp(App):

    def build(self):
        return TI()


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

它运作正常。 但是当我尝试在listview项目中编写波斯语时,它只显示黑色方块...... 我的列表项示例代码是:

# -*- coding: utf-8 -*-
import kivy
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from bidi.algorithm import get_display
import arabic_reshaper
Builder.load_string(
'''
<TI>
    but: but
    ListView:
        id: but 
''')

class TI(FloatLayout):

    def __init__(self, **kwargs):
        super(TI, self).__init__(**kwargs)
        self.but.item_strings = [get_display(arabic_reshaper.reshape(n))  for n in name]


class MyApp(App):

    def build(self):
        return TI()


if __name__ == '__main__':
MyApp().run()
问题在哪里? 有没有办法纠正它? 如何在kivy的列表视图中使用波斯语?

1 个答案:

答案 0 :(得分:0)

您必须设置字体,就像在工作示例中使用DejaVuSans一样,因为默认字体DroidSans显然不支持您的语言。您可以通过列表适配器执行此操作:

# -*- coding: utf-8 -*-
import kivy
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivy.uix.label import Label

Builder.load_string(
'''
#:import la kivy.adapters.listadapter 
#:import lbl kivy.uix.label

<TI>
    but: but
    ListView:
        id: but 
        adapter: la.ListAdapter(data=[], cls=lbl.Label)

<MyLabel>:
    font_name: 'data/fonts/DejaVuSans.ttf'
''')

class MyLabel(Label):
    pass

class TI(FloatLayout):
    def __init__(self, **kwargs):
        super(TI, self).__init__(**kwargs)
        self.but.adapter.data = [u'سلام دنیا']
        self.but.adapter.cls = MyLabel

class MyApp(App):
    def build(self):
        return TI()


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