Make a Yes No Popup with Kivy

Overview

I wanted a dialog to ask yes / no, so I made it in a form that is easy to use. Demo

code

I also put it in 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()

It's pretty simple. You can pass a callback function to the on_yes and on_no events to handle each case. It's easy to customize, so it might be useful in various situations.

Recommended Posts

Make a Yes No Popup with Kivy
Make a drawing quiz with kivy + PyTorch
Make sci-fi-like buttons with Kivy
Make a fortune with Python
Make a fire with kdeplot
Make a tky2jgd plugin with no practicality in QGIS Part 2
Make a tky2jgd plugin with no practicality in QGIS Part 1
Let's make a GUI with python.
Make a sound with Jupyter notebook
Let's make a breakout with wxPython
Make a filter with a django template
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Make a model iterator with PySide
Make a nice graph with plotly
Let's make a shiritori game with Python
Make a video player with PySimpleGUI + OpenCV
Make a rare gacha simulator with Flask
Make a Notebook Pipeline with Kedro + Papermill
Make a partially zoomed figure with matplotlib
Let's make a voice slowly with Python
Make a cascade classifier with google colaboratory
Let's make a simple language with PLY 1
Make a logic circuit with a perceptron (multilayer perceptron)
Make a wash-drying timer with a Raspberry Pi
Make a GIF animation with folder monitoring
Let's make a web framework with Python! (1)
Let's make a tic-tac-toe AI with Pylearn 2
Make a desktop app with Python with Electron
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
A memorandum to make WebDAV only with nginx
Investment quest: Make a system trade with pyhton (2)
Make a Twitter trend bot with heroku + Python
Make a monitoring device with an infrared sensor
Make a simple pixel art generator with Flask
Investment quest: Make a system trade with pyhton (1)
How to make a dictionary with a hierarchical structure.
Try to make a "cryptanalysis" cipher with Python
[Python] Make a simple maze game with Pyxel
Let's replace UWSC with Python (5) Let's make a Robot
Try to make a dihedral group with Python
How to create a multi-platform app with kivy
[Chat De Tornado] Make a chat using WebSocket with Tornado
Make holiday data into a data frame with pandas
Make a LINE WORKS bot with Amazon Lex
I tried using a database (sqlite3) with kivy
(Memorandum) Make a 3D scatter plot with matplodlib
Make one repeating string with a Python regular expression.
Make a morphological analysis bot loosely with LINE + Flask
Try to make a command standby tool with python
[Practice] Make a Watson app with Python! # 2 [Translation function]
[Practice] Make a Watson app with Python! # 1 [Language discrimination]
[Let's play with Python] Make a household account book
How to make a shooting game with toio (Part 1)
Let's make a simple game with Python 3 and iPhone
Make a breakpoint on the c layer with python
Make a function to describe Japanese fonts with OpenCV
Let's make dependency management with pip a little easier
Make a CSV formatting tool with Python Pandas PyInstaller
Let's make a Mac app with Tkinter and py2app