2
5

More than 5 years have passed since last update.

【Python Kivy】簡単なpop up windowの作成方法

Last updated at Posted at 2017-06-16

Kvファイルでサイズとかを調整したい

なかなかkvファイルでテキストとかを調整する方法がネット上に落ちていなかったので、kivyの本家サイトのサンプル(https://pyky.github.io/kivy-doc-ja/examples/gen__demo__showcase__main__py.html#file-demo-showcase-showcase-kv)
を参考にしながら(ほぼパクリながら)、以下のスクリプトを作りました。

main.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from kivy.app import App
from kivy.config import Config
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty

#force size of your window
Config.set('graphics','width','800')
Config.set('graphics','height','800')

class A_Button(Widget):
    popup = ObjectProperty()

class Example(App):
    def build(self):
        return A_Button()

if __name__ == '__main__':
        Example().run()
example.kv
<A_Button>:
    popup: pop

    BoxLayout:
        id: top

        Popup:
            id: pop
            title: "Hello World"
            on_parent:
                if self.parent == top: self.parent.remove_widget(self)
            Button:
                text: 'press to dismiss'
                on_release: pop.dismiss()
        Button:
            text: 'press to show Popup'
            on_release: root.popup.open()

kvファイルはほぼ、showcaseのサンプルからパクってきました。レイアウトについては、動作に着目したのでこだわっていません。
なんとなくObjectPropertyをつかってkvファイルに渡すイメージなんでやったらできました。

ObjectPropertyなどまだまだ勉強していかなければいけないことが多いなぁと思いました。

2
5
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
5