'How to display Hebrew text in a TextInput field in Python Kivy
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: 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...
Solution 1:[1]
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.
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tal Moshel |
Solution 2 | Yinon |