问题
I am going to make a application with kivymd, which contains Arabic and Persian texts. According to my searches, to do this, you should use arabic_reshaper and bidi.algorithm and also use a font that supports Persian and Arabic languages. As a result, I was able to write the code like this, and it supports both Persian and Arabic text well.
import kivy.app
import kivy.uix.label
import arabic_reshaper
import bidi.algorithm
class TestApp(kivy.app.App):
def build(self):
bidi_text = bidi.algorithm.get_display(arabic_reshaper.reshape("میلاد"))
return kivy.uix.label.Label(text=bidi_text, font_name="arial" , font_size="90sp")
testApp = TestApp()
testApp.run()
see the output
Now, My problem is that I want to code my program as follows, in this method I can change the font, but I can not use the method arabic_reshaper and bidi.algorithm, and this causes the output text to be displayed in this way .
from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivymd.uix.label import MDLabel
from kivy.lang import Builder
import arabic_reshaper
import bidi.algorithm
screen_helper_up = """
Screen:
NavigationLayout:
ScreenManager:
Screen:
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'میلاد'
font_name:'arial.ttf'
left_action_items: [["menu", lambda x: nav_drawer.toggle_nav_drawer()]]
elevation:10
Widget:
Label:
text: "میلاد"
font_name:'arial.ttf'
markup: True
font_size: 100
color: 0,0,0,1
MDNavigationDrawer:
id: nav_drawer
"""
class DemoApp (MDApp):
def build(self):
screen = Screen()
screen = Builder.load_string(screen_helper_up)
return screen
DemoApp().run()
As you can see in the image below, the font has changed only in Label and in MDToolbar title, Unfortunately, the font has not changed either.
see the output
Please guide me:
1- How can I use the ... and ... methods to display Persian and Arabic text in the above code?
2- How can I change the font in ...?
Thank You for Your Attention
回答1:
This is what I have done: In the Kv language, refer the text to a variable (app.res3
) in the main app:
`text:app.res3`
`font_name:'PTBLDHAD'`
In the main app, I applied the reshaper module as follows:
`text3 = ("الصفحة الرئيسية")`
reshaped_texts3 = arabic_reshaper.reshape(text3)
res3 = get_display(reshaped_texts3)
回答2:
@aiyad-alreshidi Thanks for the reply I edited the code as you said (of course if I got it right) and changed it to this code:
from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivy.lang import Builder
import arabic_reshaper
import bidi.algorithm
screen_helper_up = """
Screen:
NavigationLayout:
ScreenManager:
Screen:
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: app.res3
font_name:'iran.ttf'
left_action_items: [["menu", lambda x: nav_drawer.toggle_nav_drawer()]]
elevation:10
MDTextField:
text: app.res3
helper_text: app.res3
hint_text: app.res3
font_name: 'iran.ttf'
helper_text_mode: "persistent"
font_size: 30
line_color_focus: self.theme_cls.opposite_bg_normal
pos_hint: {'center_x': 0.5, 'center_y': 0.3}
size_hint: (0.5,0.4)
icon_right: "android"
Widget:
Label:
text: app.res3
font_name:'iran.ttf'
markup: True
font_size: 100
color: 0,0,0,1
MDNavigationDrawer:
id: nav_drawer
"""
class DemoApp (MDApp):
text3 = ("میلاد")
reshaped_texts3 = arabic_reshaper.reshape(text3)
res3 = bidi.algorithm.get_display(reshaped_texts3)
def build(self):
screen = Screen()
screen = Builder.load_string(screen_helper_up)
return screen
DemoApp().run()
Unfortunately, As you see in the image below, the font has changed only in Label and in MDToolbar title or also MDTextField (helper_text and hint_text), Again, it has not changed
来源:https://stackoverflow.com/questions/63462947/using-arabic-reshaper-and-bidi-algorithm-in-kivymd