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 |
BoxLayout (Python)
BoxLayout は、垂直または水平のボックス内に子ウィジェットを配置します。
kivy_boxlayout.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 japanize_kivy | |
from kivy.app import App | |
from kivy.core.window import Window | |
from kivy.uix.boxlayout import BoxLayout | |
from kivy.uix.button import Button | |
Window.size = (200, 200) | |
class KivyBoxLayout(BoxLayout): | |
def __init__(self): | |
super().__init__() | |
self.orientation = 'vertical' | |
self.init_ui() | |
def init_ui(self): | |
btn_a = Button(text='A', size_hint=(1, .2)) | |
btn_a.bind(on_press=self.on_button_pressed) | |
self.add_widget(btn_a) | |
btn_b = Button(text='B', size_hint=(1, .3)) | |
btn_b.bind(on_press=self.on_button_pressed) | |
self.add_widget(btn_b) | |
layout = BoxLayout(orientation='horizontal', size_hint=(1, .5)) | |
self.add_widget(layout) | |
btn_c = Button(text='C', size_hint=(.2, 1)) | |
btn_c.bind(on_press=self.on_button_pressed) | |
layout.add_widget(btn_c) | |
btn_d = Button(text='D', size_hint=(.3, 1)) | |
btn_d.bind(on_press=self.on_button_pressed) | |
layout.add_widget(btn_d) | |
btn_e = Button(text='E', size_hint=(.5, 1)) | |
btn_e.bind(on_press=self.on_button_pressed) | |
layout.add_widget(btn_e) | |
def on_button_pressed(self, instance): | |
print('ボタン %s がクリックされました。' % instance.text) | |
class ExampleApp(App): | |
def build(self): | |
self.title = 'BoxLayout' | |
return KivyBoxLayout() | |
if __name__ == '__main__': | |
ExampleApp().run() |
kivy_boxlayout.py の実行例
BoxLayout (Python + Kv)
指定したファイルを読み込むようにしたかったので、ここでは Builder.load_file() を使って、指定したファイルを読み込むようにしています。
kivy_boxlayout_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 japanize_kivy | |
from kivy.app import App | |
from kivy.core.window import Window | |
from kivy.lang import Builder | |
from kivy.uix.boxlayout import BoxLayout | |
Builder.load_file('kivy_boxlayout_1.kv') | |
Window.size = (200, 200) | |
class KivyBoxLayout(BoxLayout): | |
def __init__(self): | |
super().__init__() | |
self.orientation = 'vertical' | |
def on_button_pressed(self, instance): | |
print('ボタン %s がクリックされました。' % instance.text) | |
class ExampleApp(App): | |
def build(self): | |
self.title = 'BoxLayout' | |
return KivyBoxLayout() | |
if __name__ == '__main__': | |
ExampleApp().run() |
kivy_boxlayout_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
<KivyBoxLayout>: | |
orientation: 'vertical' | |
size: root.size | |
Button: | |
id: btn_a | |
text: "A" | |
size_hint: (1, .2) | |
on_press: root.on_button_pressed(self) | |
Button: | |
id: btn_b | |
text: "B" | |
size_hint: (1, .3) | |
on_press: root.on_button_pressed(self) | |
BoxLayout: | |
id: layout | |
size_hint: (1, .5) | |
orientation: 'horizontal' | |
Button: | |
id: btn_c | |
text: "C" | |
size_hint: (.2, 1) | |
on_press: root.on_button_pressed(self) | |
Button: | |
id: btn_d | |
text: "D" | |
size_hint: (.3, 1) | |
on_press: root.on_button_pressed(self) | |
Button: | |
id: btn_e | |
text: "E" | |
size_hint: (.5, 1) | |
on_press: root.on_button_pressed(self) | |
kivy_boxlayout_1.py の実行例
参考サイト
- bitWalk's: Kivy をちょろっと使ってみた [2022-07-10]
- Kv language — Kivy 2.1.0 documentation
- Widgets — Kivy 2.1.0 documentation
- Box Layout — Kivy 2.1.0 documentation

にほんブログ村

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