在kivymd中使用arabic_整形器和bidi.算法

2024-05-18 16:17:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我将使用kivymd制作一个应用程序,它包含阿拉伯语和波斯语文本

根据我的搜索,要做到这一点,你应该使用阿拉伯语和bidi.algorithm,还应该使用支持波斯语和阿拉伯语的字体。 因此,我能够像这样编写代码,并且它很好地支持波斯语和阿拉伯语文本

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

现在,我的问题是我想按如下方式编码我的程序,在这种方法中我可以更改字体,但我不能使用arabic_Reformer和bidi.algorithm方法,这会导致输出文本以这种方式显示

                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()

如下图所示,字体仅在Label和MDToolbar标题中更改,不幸的是,字体也没有更改

see the output

  1. 我如何使用。。。和在上述代码中显示波斯语和阿拉伯语文本的方法

  2. 如何更改…中的字体


Tags: textfrom文本importapp字体screenalgorithm
2条回答

这就是我所做的:在Kv语言中,将文本引用到主应用程序中的变量(app.res3):

`text:app.res3`

`font_name:'PTBLDHAD'`  

在主应用程序中,我应用了整形器模块,如下所示:

`text3 = ("الصفحة الرئيسية")` 

reshaped_texts3 = arabic_reshaper.reshape(text3)

res3 = get_display(reshaped_texts3)

@aiyad alreshidi 谢谢你的回复 我按照您所说的编辑了代码(当然,如果我没有弄错的话),并将其更改为以下代码:

            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()

不幸的是,如下图所示,字体仅在Label和MDToolbar标题或MDTextField(helper_text和hint_text)中更改,同样,它没有更改

see the output

相关问题 更多 >

    热门问题