如何在Kivy中使用希伯来语字体?

问题描述 投票:0回答:1

我尝试在kivy应用程序中使用希伯来语字体,在探索之后,我使用LabelBase.register并更改了我的编码为'utf-8'的kv文件的读数,但仍然出现以下错误:font_name。我的main.py:

# main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.core.text import LabelBase
LabelBase.register(name="Arial", fn_regular="Arial.ttf")

class exist_form_window(Screen):
    project_name = ObjectProperty(None)
    manage_name = ObjectProperty(None)

    def create(self):
        if (self.project_name.text):
            self.reset()
            sm.current = "main"
        else:
            invalidLogin()  

    def reset(self):
        self.project_name.text =""
        self.manage_name.text = ""



class new_form_window(Screen):
    project_name = ObjectProperty(None)
    manage_name = ObjectProperty(None)

    def create(self):
        if (self.project_name.text):
            self.reset()
            sm.current = "main"
        else:
            invalidLogin()  

    def reset(self):
        self.project_name.text =""
        self.manage_name.text = ""

class MainWindow(Screen):
    def new_form(self):
        sm.current = "new_form"
    def exist_form(self):
        sm.current = "exist_form"

class WindowManager(ScreenManager):
    pass

def invalidLogin():
    pop = Popup(title='שגיאה',
                  content=Label(text='חלק מהערכים חסרים'),
                  size_hint=(None, None), size=(400, 400))
    pop.open()

with open("roniapp.kv", encoding='utf-8') as f:
    Builder.load_string(f.read())

sm = WindowManager()

screens = [MainWindow(name="main"),new_form_window(name="new_form"),exist_form_window(name="exist_form")]

for screen in screens:
    sm.add_widget(screen)

sm.current = "main"


class MyMainApp(App):
    def build(self):
        return sm


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

我的roniapp.kv文件:

<MainWindow>:
name: "main"

FloatLayout:
    Label:
    font_name: 'Arial'  
        text:"Asad"
        font_size: (root.width**2 + root.height**2) / 13**4
        pos_hint: {"x":0.1, "top":0.9}
        size_hint: 0.35, 0.15
    Button:
        pos_hint:{"x":0.2, "y": 0.3}
        size_hint:0.6,0.2
        text: "aaa"
        on_release:
            root.new_form()
            root.manager.transition.direction = "down"

    Button:
        pos_hint:{"x":0.2, "y": 0.1}
        size_hint:0.6,0.2
        text: "aaa"
        on_release:
            root.exist_form()
            root.manager.transition.direction = "left"



<new_form_window>:
name: "new_form"

project_name: project_name
manage_name: manage_name

FloatLayout:

    Label:
        text:"aaa"
        font_size: (root.width**2 + root.height**2) / 13**4
        pos_hint: {"x":0.1, "top":0.9}
        size_hint: 0.35, 0.15

    TextInput:
        id: project_name
        font_size: (root.width**2 + root.height**2) / 13**4
        multiline: False
        pos_hint: {"x": 0.45 , "top":0.9}
        size_hint: 0.4, 0.15

    Label:
        text:"aaa"
        font_size: (root.width**2 + root.height**2) / 13**4
        pos_hint: {"x":0.1, "top":0.7}
        size_hint: 0.35, 0.15

    TextInput:
        id: manage_name
        font_size: (root.width**2 + root.height**2) / 13**4
        multiline: False
        pos_hint: {"x": 0.45, "top":0.7}
        size_hint: 0.4, 0.15

    Button:
        pos_hint:{"x":0.2,"y":0.05}
        size_hint: 0.6, 0.2
        font_size: (root.width**2 + root.height**2) / 13**4
        text: "aaa"
        on_release:
            root.manager.transition.direction = "left"
            root.create()

<exist_form_window>:
name: "exist_form"

project_name: project_name
manage_name: manage_name

FloatLayout:

    Label:
        text:"aaa"
        font_size: (root.width**2 + root.height**2) / 13**4
        pos_hint: {"x":0.1, "top":0.9}
        size_hint: 0.35, 0.15

    TextInput:
        id: project_name
        font_size: (root.width**2 + root.height**2) / 13**4
        multiline: False
        pos_hint: {"x": 0.45 , "top":0.9}
        size_hint: 0.4, 0.15

    Label:
        text:"aaa"
        font_size: (root.width**2 + root.height**2) / 13**4
        pos_hint: {"x":0.1, "top":0.7}
        size_hint: 0.35, 0.15

    TextInput:
        id: manage_name
        font_size: (root.width**2 + root.height**2) / 13**4
        multiline: False
        pos_hint: {"x": 0.45, "top":0.7}
        size_hint: 0.4, 0.15

    Button:
        pos_hint:{"x":0.2,"y":0.05}
        size_hint: 0.6, 0.2
        font_size: (root.width**2 + root.height**2) / 13**4
        text: "aaa"
        on_release:
            root.manager.transition.direction = "left"
            root.create()

并得到此错误:

File "C:\Users\adi\Anaconda3\envs\project_2\lib\site-packages\kivy\lang\parser.py", line 584, in 
parse_level
 'Invalid data after declaration')

ParserException: Parser: File "<inline>", line 6:
...
      4:    FloatLayout:
      5:        Label:
>>    6:        font_name: 'Arial'  
      7:            text:"Asad"
      8:            font_size: (root.width**2 + root.height**2) / 13**4
...
Invalid data after declaration

如果是因为我用记事本保存了kv文件,我尝试使用其他应用程序仍然出现此错误。

感谢您的帮助!

kivy kivy-language
1个回答
0
投票

kivy解析器抛出错误,因为kv文件中的代码格式不正确。您的indentation需要修复。我设法通过保持python代码不变并用以下代码替换您的kv文件在计算机上运行您的应用程序:

#:kivy 1.1.1
# replace the above with your kivy version
<MainWindow>:
    name: "main"
    FloatLayout:
        Label:
            font_name: 'Arial'  
            text:"Asad"
            font_size: (root.width**2 + root.height**2) / 13**4
            pos_hint: {"x":0.1, "top":0.9}
            size_hint: 0.35, 0.15
        Button:
            pos_hint:{"x":0.2, "y": 0.3}
            size_hint:0.6,0.2
            text: "aaa"
            on_release:
                root.new_form()
                root.manager.transition.direction = "down"

        Button:
            pos_hint:{"x":0.2, "y": 0.1}
            size_hint:0.6,0.2
            text: "aaa"
            on_release:
                root.exist_form()
                root.manager.transition.direction = "left"



<new_form_window>:
    name: "new_form"
    project_name: project_name
    manage_name: manage_name
    FloatLayout:
        Label:
            text:"aaa"
            font_size: (root.width**2 + root.height**2) / 13**4
            pos_hint: {"x":0.1, "top":0.9}
            size_hint: 0.35, 0.15

        TextInput:
            id: project_name
            font_size: (root.width**2 + root.height**2) / 13**4
            multiline: False
            pos_hint: {"x": 0.45 , "top":0.9}
            size_hint: 0.4, 0.15

        Label:
            text:"aaa"
            font_size: (root.width**2 + root.height**2) / 13**4
            pos_hint: {"x":0.1, "top":0.7}
            size_hint: 0.35, 0.15

        TextInput:
            id: manage_name
            font_size: (root.width**2 + root.height**2) / 13**4
            multiline: False
            pos_hint: {"x": 0.45, "top":0.7}
            size_hint: 0.4, 0.15

        Button:
            pos_hint:{"x":0.2,"y":0.05}
            size_hint: 0.6, 0.2
            font_size: (root.width**2 + root.height**2) / 13**4
            text: "aaa"
            on_release:
                root.manager.transition.direction = "left"
                root.create()

<exist_form_window>:
    name: "exist_form"
    project_name: project_name
    manage_name: manage_name
    FloatLayout:
        Label:
            text:"aaa"
            font_size: (root.width**2 + root.height**2) / 13**4
            pos_hint: {"x":0.1, "top":0.9}
            size_hint: 0.35, 0.15

        TextInput:
            id: project_name
            font_size: (root.width**2 + root.height**2) / 13**4
            multiline: False
            pos_hint: {"x": 0.45 , "top":0.9}
            size_hint: 0.4, 0.15

        Label:
            text:"aaa"
            font_size: (root.width**2 + root.height**2) / 13**4
            pos_hint: {"x":0.1, "top":0.7}
            size_hint: 0.35, 0.15

        TextInput:
            id: manage_name
            font_size: (root.width**2 + root.height**2) / 13**4
            multiline: False
            pos_hint: {"x": 0.45, "top":0.7}
            size_hint: 0.4, 0.15

        Button:
            pos_hint:{"x":0.2,"y":0.05}
            size_hint: 0.6, 0.2
            font_size: (root.width**2 + root.height**2) / 13**4
            text: "aaa"
            on_release:
                root.manager.transition.direction = "left"
                root.create()
© www.soinside.com 2019 - 2024. All rights reserved.