我幸运地找到了一个答案并尝试了一下。
链接在这里:https://github.com/hosseinofj/persian_textinput_kivy/blob/master/codes
因为它被删除了,所以我自己来解释一下,尽管发帖的用户也应该得到某种感谢!
无论如何,这是代码:
test.kv
<Ar_text@TextInput>:
text: "whatever"
multiline: 0
size_hint: 1,1
font_name: "data/unifont-11.0.02.ttf"
font_size: 26
padding_y: [15,0]
padding_x: [self.size[0]-self._get_text_width(max(self._lines, key=len), self.tab_width, self._label_cached)-10,8]
main.py
'''
App demonstrating a Text input field which accepts Arabic script in kivy
'''
import arabic_reshaper
from bidi.algorithm import get_display
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty, NumericProperty, StringProperty
class Ar_text(TextInput):
max_chars = NumericProperty(20)
str = StringProperty()
def __init__(self, **kwargs):
super(Ar_text, self).__init__(**kwargs)
self.text = get_display(arabic_reshaper.reshape("اطبع شيئاً"))
def insert_text(self, substring, from_undo=False):
if not from_undo and (len(self.text) + len(substring) > self.max_chars):
return
self.str = self.str+substring
self.text = get_display(arabic_reshaper.reshape(self.str))
substring = ""
super(Ar_text, self).insert_text(substring, from_undo)
def do_backspace(self, from_undo=False, mode='bkspc'):
self.str = self.str[0:len(self.str)-1]
self.text = get_display(arabic_reshaper.reshape(self.str))
class TestApp(App):
def build(self):
return Ar_text()
if __name__ == '__main__':
TestApp().run()
这个技巧是使用
arabic_shaper
。因此,在每次更新文本时,您需要使用
arabic_shaper
提供一个格式化的字符串作为文本输入。
仍然存在一个根本性问题,即真正的RTL不存在(光标始终在字符串末尾的右侧)。
我添加了一个带有示例代码的存储库。它在Ubuntu上运行。如果正确安装了Kivy,它也应该可以在Windows上运行。
https://github.com/eliasaj92/arabic_text_input_kivy