LoginSignup
0
2

More than 3 years have passed since last update.

KivyでYes No Popupを作る

Posted at

概要

はい・いいえを聞くダイアログが欲しかったので、使いまわしやすいような形で作りました。
Demo

コード

gitにも置きました。

__init__.py
'''
    a simple Yes/No Popup
    LICENSE : MIT
'''
from kivy.uix.popup import Popup
from kivy.properties import StringProperty

from kivy.lang.builder import Builder
Builder.load_string('''
#<KvLang>
<YesNoPopup>:
    FloatLayout:
        Label:
            size_hint: 0.8, 0.6
            pos_hint: {'x': 0.1, 'y':0.4}
            text: root.message

        Button:
            size_hint: 0.4, 0.35
            pos_hint: {'x':0.1, 'y':0.05}
            text: 'Yes'
            on_release: root.dispatch('on_yes')

        Button:
            size_hint: 0.4, 0.35
            pos_hint: {'x':0.5, 'y':0.05}
            text: 'No'
            on_release: root.dispatch('on_no')

#</KvLang>
''')

class YesNoPopup(Popup):
    __events__ = ('on_yes', 'on_no')

    message = StringProperty('')

    def __init__(self, **kwargs) -> None:
        super(YesNoPopup, self).__init__(**kwargs)
        self.auto_dismiss = False

    def on_yes(self):
        pass

    def on_no(self):
        pass


if __name__ == '__main__':
    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.button import Button

    class TestApp(App):
        def __init__(self, **kwargs):
            super(TestApp, self).__init__(**kwargs)

        def build(self):
            self.pop = pop = YesNoPopup(
                title='Popup !',
                message='OK ?',
                size_hint=(0.4, 0.3),
                pos_hint={'x':0.3, 'y':0.35}
            )
            pop.bind(
                on_yes=self._popup_yes,
                on_no=self._popup_no
            )
            root = BoxLayout()
            btn = Button(text='open')
            root.add_widget(btn)
            btn.bind(on_release=lambda btn: self.pop.open())
            return root

        def _popup_yes(self, instance):
            print(f'{instance} on_yes')
            self.pop.dismiss()

        def _popup_no(self, instance):
            print(f'{instance} on_no')
            self.pop.dismiss()


    TestApp().run()

結構シンプルです。on_yesとon_noイベントにそれぞれの場合の処理を行うコールバック関数を渡せばよいでしょう。
カスタマイズも容易なので、いろいろな場面で使えそう、、かも?

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