2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Kivyを使ってみた(Python)

Posted at

はじめに

KivyというGUIライブラリを作成するオープンソースのライブラリを使ってみたので、まとめました

前提

・Pythonがインストールされていること
・Homebrewがインストールされていること
・MacOSのPCを使用すること

Kivyのインストール

1.Kivyに必要な依存関係のあるものをインストールします

brew install pkg-config sdl2_image sdl2_ttf sdl2_mixer gstreamer

2.Cythonをインストールします

pip3 install -U Cython

3.Kivyをインストールします

pip3 install kivy

4.Kivyの日本語版をインストールします

pip3 install japanize-kivy

簡単なアプリ実行

1.sample.pyファイルを作成します
作成したファイルに下記コードをコピペしてください

sample.py
from kivy.app import App
from kivy.uix.label import Label
import japanize_kivy

class Test(App):
    def build(self):
        return Label(
            text = "テスト実行",
            font_size = 60
        )

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

2.動かしてみます

python3 sample.py

3.数分待ち、下記のような画面が表示されればOK
スクリーンショット 2022-07-06 11.11.18.png

GUIアプリ作成

1.gui_apl.pyファイルを作成します
作成したファイルに下記コードをコピペしてください

gui_apl.py
from kivy.app import App
import japanize_kivy
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.core.window import Window

Window.clearcolor = (1, 1, 1, 1)

# 画面
class Screen(App):
    def build(self):
        # 画面の設定
        self.window = GridLayout()
        self.window.cols = 1
        self.window.size_hint = (0.6, 0.7)
        self.window.pos_hint = {'center_x':0.5, 'center_y':0.5}

        # ウィジェットの追加
        self.window.add_widget(Image(source='sample.jpeg'))

        # ラベル
        self.Test = Label(
            # ラベルのテキスト
            text = 'サンプルアプリ',
            # フォントサイズ
            font_size = 40,
            # 文字色
            color = 'blue'
        )

        # ラベルを画面上部に追加
        self.window.add_widget(self.Test)

        # ボタンウィジェット
        self.button = Button(
            # ボタン上に表示させるのテキスト
            text = 'クリック',
            # 背景色
            background_color = 'blue',
            # サイズの設定
            size_hint = (1,0.5),
            # 文字色
            color = 'white',
            # フォントサイズ
            font_size = 30,
            # ボタンの背景色
            background_normal = ''
        )

        # ボタンをクリックした時の処理
        self.button.bind(on_press = self.callback)
        # ボタンを画面上に追加
        self.window.add_widget(self.button)

        # レイアウトをリターン
        return self.window

    def callback(self, instance):
        # ラベルのテキストを変更
        self.Test.text = 'クリックされました'

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

2.動かしてみます

python3 gui_apl.py

3.画面が開いて、ボタンクリックイベントが動けばOK

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?