问题
I've another problem in kivy programming. I wanht to write persian in my App and in used Arabi_reshaper for it. when i try to do sth like this:
# -*- 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()
it works properly. but when i try to write persian in listview item it only show dark squares... my sample code for list item is this:
# -*- 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()
where is the problem? Is there any way to correct it? how can i use persian in list view in kivy?
回答1:
You have to set the font, just like you did in working example, where you are using DejaVuSans
, since the default font, DroidSans
, apparently doesn't support your language. You can do this through list adapter:
# -*- 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()
来源:https://stackoverflow.com/questions/27616652/wrinting-persian-in-kivy-list-view-item