Python:如何在 kivy 标签中使用 google Drive 中的自定义字体而不将其存储在本地

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

我想在android上的python模拟器中运行kivy应用程序而不访问所有文件,所以我需要它不存储在本地

我尝试使用io字节、请求等,但都无济于事。

如果有人能帮助我如何做到这一点,那就太好了。

python fonts permissions kivy label
1个回答
0
投票

在 Kivy 标签中使用 Google Drive 中的自定义字体而不将其存储在本地可能具有挑战性,因为 Kivy 通常要求字体可在本地访问,但您可以尝试此方法:

  1. 在 Google 云端硬盘上托管您的自定义字体文件,并确保共享设置设置为“知道链接的任何人都可以查看”。

  2. 从 Google Drive 获取字体文件的直接下载链接。您可以通过右键单击该文件,选择“获取可共享链接”并修改该链接以使其成为直接下载链接来完成此操作。它应该看起来像这样:

    https://drive.google.com/uc?export=download&id=YOUR_FILE_ID

  3. 请求库将从谷歌驱动器下载字体。

  4. kivy.core.text.LabelBase.register() 将加载您现在下载的字体。

from kivy.app import App
from kivy.core.text import LabelBase
import requests
from io import BytesIO

class CustomFontApp(App):
    def build(self):
        # Replace with your Google Drive font file link
        font_url = "https://drive.google.com/uc?export=download&id=YOUR_FILE_ID"
        
        # Download the font file
        response = requests.get(font_url)
        
        if response.status_code == 200:
            # Load the font file
            font_data = BytesIO(response.content)
            LabelBase.register("CustomFont", fn_regular=font_data)
            
            # Create a label using the custom font
            label = Label(text="Hello, Custom Font!", font_name="CustomFont", font_size="30sp")
            return label
        else:
            return Label(text="Font download failed.")

if __name__ == '__main__':
    CustomFontApp().run()

© www.soinside.com 2019 - 2024. All rights reserved.