文字化け問題を解決!Kivyアプリで日本語を正しく表示する方法【Python】(kivy-ios, buildozer)

Posted by


In this tutorial, we will discuss how to display Japanese text in a Kivy app on iOS using buildozer.

First, let’s talk about the issue of "mojibake" or "文字化け" which is the term used when text becomes gibberish or unreadable due to encoding issues. This is a common problem when working with Japanese text on different platforms or systems.

To display Japanese text correctly in a Kivy app on iOS, we need to make sure that the text is properly encoded and decoded. We also need to set the correct font for displaying Japanese characters.

Here are the steps to display Japanese text in a Kivy app on iOS:

Step 1: Install Kivy and buildozer
Before we begin, make sure you have Kivy and buildozer installed on your system. You can install Kivy using pip:

pip install kivy

And install buildozer using pip as well:

pip install buildozer

Step 2: Create a Kivy app
Create a new directory for your Kivy app and create a new main.py file in that directory. You can use any text editor of your choice to create the file.

Step 3: Customize your Kivy app
In the main.py file, you can customize your Kivy app by adding widgets and setting up the user interface. To display Japanese text, make sure to set the font to a font that supports Japanese characters. You can use the following code snippet to set the font:

from kivy.core.text import LabelBase

LabelBase.register(name='CUSTOM_FONT',fn_regular='path/to/custom_font.ttf')

Replace ‘path/to/custom_font.ttf’ with the path to your custom font file that supports Japanese characters.

Step 4: Display Japanese text
Now you can add Japanese text to your Kivy app using the Label widget. Make sure to encode the text properly using utf-8 encoding. Here’s an example code snippet:

from kivy.app import App
from kivy.uix.label import Label

class MyApp(App):
    def build(self):
        return Label(text=u'こんにちは、世界', font_name='CUSTOM_FONT', font_size='20sp')

MyApp().run()

Replace ‘こんにちは、世界’ with your Japanese text.

Step 5: Build and deploy your app
Now that your Kivy app is ready, you can build it for iOS using buildozer. Run the following command in your terminal:

buildozer ios debug

This command will create an iOS package for your Kivy app. You can then deploy the app to your iOS device for testing.

That’s it! You have successfully displayed Japanese text in a Kivy app on iOS using buildozer. Feel free to customize your app further and add more features. Happy coding!

0 0 votes
Article Rating
6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@user-hu9iv1vl8t
1 month ago

むっちゃ参考になりました!パスを記述するより同じディレクトリに入れるという発想が無かったので驚愕です。

@user-ws8pw7uo7v
1 month ago

python でAndroidアプリを作りたく、勉強していたところこの動画を拝見しました。
開発環境がラズパイであり、自分だけで使うためなので、携帯とUSB接続してファイル転送。kivy launcher でお手軽に動作させようとしています。
ですが、日本語フォントを取り込むとアプリが異常終了してしまいます。
この動画を見て、やはりbuildozerでの変換が必要ということなんですよね。

@user-ri6yd3ff3s
1 month ago

androidスマホで日本語を表示できました。ありがとうございました。ところがTextInputに入力しようとすると、スマホのキーボードにひらがなが表示されません。アルファベットや数字は入力できるのですが。解決できるのでしょうか・・

@bylittlewind
1 month ago

大変助かります!ちなみにKIVYってボタンの形をカスタマイズ可能ですか、ちょっと丸いボタンにやっていきたいんですが。。

@NekoSiesta
1 month ago

初めまして、一昨日からkivyをはじめ、今年のうちにアプリ開発を目指している者です。
すごく勉強になりましたので、事後報告で申し訳ないのですが、私のブログで紹介させて頂きました。

@0312zico
1 month ago

今ちょうど困ってたからめっちゃ助かります!