Tech With Tim Logo
Go back

Popup Windows

This tutorial will show you how to create a kivy popup window.

Importing Modules

The following modules will be necessary for this tutorial.

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup

Creating a Popup Window

Creating a popup window is actually quite simple.

The first thing we need to do is create a new class that will hold the content of our popup. I will call mine "P". It will inherit from FloatLayout so that I can use size_hint and pos_hint.

class P(FloatLayout):
    pass

The next step is to add the content! From within our kv file we will create a label and a button that will represent our popup.

<P>:
    Label:
        text: "You pressed the button"
        size_hint: 0.6, 0.2
        pos_hint: {"x":0.2, "top":1}

    Button:
        text: "You pressed the button"
        size_hint: 0.8, 0.2
        pos_hint: {"x":0.1, "y":0.1}

The last step is create a function somewhere within our program that will be responsible for showing the popup.

def show_popup():
    show = P() # Create a new instance of the P class 

    popupWindow = Popup(title="Popup Window", content=show, size_hint=(None,None),size=(400,400)) 
    # Create the popup window

    popupWindow.open() # show the popup

Now whenever we call the show_popup() function a popup will be displayed.

Triggering the Popup Window

Now that we have created the function show_popup() all we need to do is call it to display the popup. To accomplish this we will create a method from within our Widgets class that will simply call show_popup(). To trigger this method we will call it from the on_release event of our button.

class Widgets(Widget):
    def btn(self):
        show_popup()
<Widgets>:
    Button:
        text: "Press me"
        on_release: root.btn()

Now when we press the button we should see the popup! pressed.png

Full Code

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup


class Widgets(Widget):
    def btn(self):
        show_popup()

class P(FloatLayout):
    pass


class MyApp(App):
    def build(self):
        return Widgets()


def show_popup():
    show = P()

    popupWindow = Popup(title="Popup Window", content=show, size_hint=(None,None),size=(400,400))

    popupWindow.open()


if __name__ == "__main__":
    MyApp().run()
<Widgets>:
    Button:
        text: "Press me"
        on_release: root.btn()

<P>:
    Label:
        text: "You pressed the button"
        size_hint: 0.6, 0.2
        pos_hint: {"x":0.2, "top":1}

    Button:
        text: "You pressed the button"
        size_hint: 0.8, 0.2
        pos_hint: {"x":0.1, "y":0.1}
Design & Development by Ibezio Logo