用于阿拉伯语文本的Kivy文本输入

use*_*401 2 python arabic right-to-left kivy

我正在尝试将Kivy的文本输入用于阿拉伯语文本.我有一个阿拉伯字体设置与我的文本输入,但当我输入输入(阿拉伯语)我只是从左到右出现阿拉伯字母(并且他们没有连接,因为阿拉伯字母应该是当它们相邻时彼此).

有没有办法让Kivy /文本输入支持我缺少的RTL语言输入(特别是阿拉伯语).

这是我的代码,

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout

Config.set('graphics', 'width', '300')
Config.set('graphics', 'height', '500')


logger = logging.getLogger('')

from kivy.uix.textinput import TextInput


class EditorApp(App):
    def build(self):
        f = FloatLayout()
        textinput = TextInput(text='Hello world', font_name='DroidKufi-Regular.ttf')
        # import pdb; pdb.set_trace()

        f.add_widget(textinput)

        return f


if __name__ == '__main__':
    EditorApp().run()
Run Code Online (Sandbox Code Playgroud)

这段代码的结果:

在此输入图像描述

Big*_*her 6

不幸的是,Kivy TextInput对从右到左的支持是一个悬而未决的问题(2015年5月29日检查).实际上,Kivy不仅支持从右到左的TextInput.

对于像标签这样的静态文本,使用arabic_reshaperpython-bidi(参考)有一个hack :

import arabic_reshaper
from bidi.algorithm import get_display

reshaped_text = arabic_reshaper.reshape(u'????? ??????? ?????')
bidi_text = get_display(reshaped_text)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

然而,对于TextInput动态输入,您必须覆盖大多数类方法以支持RTL,您最终将实现对kivy的整个RTL支持.

以下是实施Kivy bidi支持的公开尝试.另一个封闭的:从右到左的标签支持.


小智 5

所以有一个答案,我很幸运能够得到并尝试。

\n\n

链接在这里:https://github.com/hosseinofj/persian_textinput_kivy/blob/master/codes

\n\n

由于它因没有解释而被删除,也没有在此处发布代码,我自己会这样做,尽管发布此链接的用户也应该以某种方式感谢!

\n\n

无论如何,这是代码:

\n\n

测试.kv

\n\n
<Ar_text@TextInput>:\n    text: "whatever"\n    multiline: 0\n    size_hint: 1,1\n    font_name: "data/unifont-11.0.02.ttf" # the font you want to use\n    font_size: 26\n    padding_y: [15,0] # can be changed\n    padding_x: [self.size[0]-self._get_text_width(max(self._lines, key=len), self.tab_width, self._label_cached)-10,8]\n
Run Code Online (Sandbox Code Playgroud)\n\n

主要.py

\n\n
\'\'\'\nApp demonstrating a Text input field which accepts Arabic script in kivy\n\n\'\'\'\n\n\nimport arabic_reshaper\nfrom bidi.algorithm import get_display\nfrom kivy.app import App\nfrom kivy.uix.textinput import TextInput\nfrom kivy.properties import ObjectProperty, NumericProperty, StringProperty\n\n\nclass Ar_text(TextInput):\n    max_chars = NumericProperty(20)  # maximum character allowed\n    str = StringProperty()\n\n    def __init__(self, **kwargs):\n        super(Ar_text, self).__init__(**kwargs)\n        self.text = get_display(arabic_reshaper.reshape("\xd8\xa7\xd8\xb7\xd8\xa8\xd8\xb9 \xd8\xb4\xd9\x8a\xd8\xa6\xd8\xa7\xd9\x8b"))\n\n\n    def insert_text(self, substring, from_undo=False):\n        if not from_undo and (len(self.text) + len(substring) > self.max_chars):\n            return\n        self.str = self.str+substring\n        self.text = get_display(arabic_reshaper.reshape(self.str))\n        substring = ""\n        super(Ar_text, self).insert_text(substring, from_undo)\n\n    def do_backspace(self, from_undo=False, mode=\'bkspc\'):\n        self.str = self.str[0:len(self.str)-1]\n        self.text = get_display(arabic_reshaper.reshape(self.str))\n\n\nclass TestApp(App):\n\n    def build(self):\n        return Ar_text()\n\n\nif __name__ == \'__main__\':\n    TestApp().run()\n
Run Code Online (Sandbox Code Playgroud)\n\n

这样做的技巧是使用arabic_shaper

\n\n

因此,在每次更新文本时,您都可以使用arabic_shaper.

\n\n

仍然存在一个根本问题,即真正的 RTL 不存在(光标始终位于字符串末尾的右侧)

\n\n

我添加了一个带有示例代码的存储库。它在 Ubuntu 上运行。如果正确安装了 Kivy,它也应该在 Windows 上运行

\n\n

https://github.com/eliasaj92/arabic_text_input_kivy

\n