Kivy は、NUI (Natural User Interface) を持つモバイルアプリやその他のマルチタッチアプリケーションソフトウェアを開発するためのフリーでオープンソースの Python フレームワークです。MIT ライセンスのもとで配布され、Android, iOS, Linux, macOS そして Windows で動作させることができます。
Wikipedia より引用、翻訳、編集
使ったことのなかった Python の GUI ライブラリ(フレームワーク)、Kivy に興味を持ったので [1]、ひととおりウィジェットのサンプルを作ってみようとしています。サンプルを作っていくにあたって、どんなスタイルでコーディングするか、テンプレートみたいなものを固めていこうとあれこれ試し始めました。
今回はフロートレイアウトのサンプルを紹介します。
Python のコードのみのサンプルと、UI 部分を分離して Kv 言語で記述した同じ動作をするサンプルを併せて紹介しています。
下記の環境で動作確認をしています。
![]() |
Fedora Linux 36 | x86_64 |
python3 | 3.10.6-1.fc36.x86_64 | |
Kivy | 2.1.0 |
FloatLayout (Python)
FloatLayout は、子ウィジェットのプロパティの pos_hint と size_hint に従って、そのウィジェットを配置します。
kivy_floatlayout.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import kivy | |
import japanize_kivy | |
from kivy.app import App | |
from kivy.core.window import Window | |
from kivy.uix.button import Button | |
from kivy.uix.floatlayout import FloatLayout | |
Window.size = (200, 200) | |
class KivyFloatLayout(FloatLayout): | |
def __init__(self): | |
super().__init__() | |
self.init_ui() | |
def init_ui(self): | |
btn_a = Button( | |
text='ボタンA', | |
size_hint=(.4, .3), | |
pos_hint={'x': .1, 'y': .6} | |
) | |
btn_a.bind(on_press=self.on_button_pressed) | |
self.add_widget(btn_a) | |
btn_b = Button( | |
text='ボタンB', | |
size_hint=(.4, .3), | |
pos_hint={'x': .5, 'y': .1} | |
) | |
btn_b.bind(on_press=self.on_button_pressed) | |
self.add_widget(btn_b) | |
def on_button_pressed(self, instance): | |
print('%s がクリックされました。' % instance.text) | |
class ExampleApp(App): | |
def build(self): | |
self.title = 'FloatLayout' | |
return KivyFloatLayout() | |
if __name__ == '__main__': | |
# version information | |
print(sys.version) | |
print(kivy.__version__) | |
# | |
ExampleApp().run() |
kivy_floatlayout.py の実行例
FloatLayout (Python + Kv)
指定したファイルを読み込むようにしたかったので、ここでは Builder.load_file() を使って、指定したファイルを読み込むようにしています。
kivy_floatlayout_1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import kivy | |
import japanize_kivy | |
from kivy.app import App | |
from kivy.core.window import Window | |
from kivy.lang import Builder | |
from kivy.uix.floatlayout import FloatLayout | |
Builder.load_file('kivy_floatlayout_1.kv') | |
Window.size = (200, 200) | |
class KivyFloatLayout(FloatLayout): | |
def on_button_pressed(self, instance): | |
print('%s がクリックされました。' % instance.text) | |
class ExampleApp(App): | |
def build(self): | |
self.title = 'FloatLayout' | |
return KivyFloatLayout() | |
if __name__ == '__main__': | |
# version information | |
print(sys.version) | |
print(kivy.__version__) | |
# | |
ExampleApp().run() |
kivy_floatlayout_1.kv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<KivyFloatLayout>: | |
size: root.size | |
Button: | |
id: btn_a | |
text: "ボタンA" | |
size_hint:(.4, .3) | |
pos_hint:{'x': .1, 'y': .6} | |
on_press: root.on_button_pressed(self) | |
Button: | |
id: btn_b | |
text: "ボタンB" | |
size_hint:(.4, .3) | |
pos_hint:{'x': .5, 'y': .1} | |
on_press: root.on_button_pressed(self) |
kivy_floatlayout_1.py の実行例
参考サイト
- bitWalk's: Kivy をちょろっと使ってみた [2022-07-10]
- Kv language — Kivy 2.1.0 documentation
- Widgets — Kivy 2.1.0 documentation
- Float Layout — Kivy 2.1.0 documentation

にほんブログ村

0 件のコメント:
コメントを投稿