How to display Hebrew text in a TextInput field in Python Kivy

Question:

I am trying to make a simple app in kivy(a python package) that gets a text from a TextInput field and when a button is clicked it returns a text in Hebrew that will displayed on another TextInput, Everything seems to be working just fine but I encounter the problem that a TextInput field in Kivy could not show the Hebrew text I am trying to show.

This is what I get:
What I get
As you can see, It shows this weird text instead of the text I need to show…

My code, My main script:

import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
import getData

class MainScreen(Widget):
    ttc = ObjectProperty(None)
    ct = ObjectProperty(None)

    def btn(self):
        self.ct.text = getData.HE_EN(text=self.ttc.text.lower())

    pass
class MyApp(App):
    def build(self):
        return MainScreen()

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

My “my.kv” file:

<MainScreen>:
    ttc: ttc
    ct: ct
    GridLayout:
        size: root.width, root.height
        cols: 1
        TextInput:
            text: ""
            id: ttc
        Button:
            text: "CONVERT"
            on_press: root.btn()
        TextInput:
            text: "CONVERTED TEXT"
            id: ct

There is no need to show the getData.py script that returns the text in Hebrew because it doesn’t really matter…

The expected result is to get the text I want in the TextInput even thought I don’t really manage to.
Please help me fixing my issue, I really do need that…

Asked By: Tal Moshel

||

Answers:

Okay! So it didn’t take a long time because someone on a discord server helped me and all I had to do was to just switch the text area font because the previous one didn’t have an Hebrew font. To do it I downloaded the font “Arial” added it to my folder with the main script, I imported from kivy.core.text import LabelBase and then registered the font: LabelBase.register(name="Arial", fn_regular="Arial.ttf"), To tell the TextInput that I want to set the font to that I just added to my .kv file under the widget ‘font_name: “Arial”‘ and that solved the problem.

Answered By: Tal Moshel

you should also reverse the text that the user type, i did this:

class HebrowTextInput(TextInput):

    def __init__(self, **kwargs):
        super(HebrowTextInput, self).__init__(font_name='DejaVuSans.ttf', halign="right", **kwargs)
        self.multiline = False

    def keyboard_on_key_down(self, window, keycode, text, modifiers):
        if keycode[1] == "backspace":
            self.text = self.text[1:]

    def insert_text(self, theText, from_undo=False):
        self.text = theText + self.text
Answered By: Yinon

To get Hebrew to show you do not need to download any font. The default font doesn’t support Hebrew, so you need to choose a a font that does like "Arial".
Also you will have to flip all your Hebrew strings to show right to left properly.
Here is a simple Hebrew "Hello world" app:

import kivy
kivy.require('2.1.0') # replace with your current kivy version !

from kivy.app import App
from kivy.uix.label import Label

def RTL(hebrew_str):
# convert string from LTR to RTL
    return hebrew_str[::-1]
my_font = 'Arial' # must be a Hebrew supportting font 

class my_app(App):
    def build(self):
        return Label(text=RTL('שלום עולם'), font_name=my_font)

if __name__ == '__main__':
    my_app().run()
Answered By: adiro
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.