Python | Popup widget in Kivy

Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.

Popup widget :

  • The Popup widget is used to create popups. By default, the popup will cover the whole “parent” window. When you are creating a popup, you must at least set a Popup.title and Popup.content.
  • Popup dialogs are used ]when we have to convey certain obvious messages to the user. Messages to the user through status bars as well for specific messages which need to be told with emphasis can still be done through popup dialogs.
  • Keep one thing in mind that the default size of a widget is size_hint=(1, 1).
  • If you don’t want your popup to be on the full screen you must gave either size hints with values less than 1 (for instance size_hint=(.8, .8)) or deactivate the size_hint and use fixed size attributes.

To use popup you must have to import :

from kivy.uix.popup import Popup

Note: Popup is a special widget. Don’t try to add it as a child to any other widget. If you do, Popup will be handled like an ordinary widget and won’t be created hidden in the background.

Basic Approach :

1) import kivy
2) import kivyApp
3) import Label
4) import button
5) import Gridlayout
6) import popup
7) Set minimum version(optional)
8) create App class
9) return Layout/widget/Class(according to requirement)
10) In the App class create the popup
11) Run an instance of the class
Code #1: In the first code the popup will cover the whole “parent” window.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Kivy example for the Popup widget
  
# Program to Show how to create a switch
# import kivy module   
import kivy 
     
# base Class of your App inherits from the App class.   
# app:always refers to the instance of your application  
from kivy.app import App
   
# this restrict the kivy version i.e 
# below this kivy version you cannot 
# use the app or software 
kivy.require('1.9.0')
  
# The Button is a Label with associated actions
# that is triggered when the button
# is pressed (or released after a click/touch).
from kivy.uix.button import Button
  
  
# The GridLayout arranges children in a matrix.
# It takes the available space and
# divides it into columns and rows,
# then adds widgets to the resulting “cells”.
from kivy.uix.gridlayout import GridLayout
  
# Popup widget is used to create popups.
# By default, the popup will cover
# the whole “parent” window.
# When you are creating a popup,
# you must at least set a Popup.title and Popup.content.
from kivy.uix.popup import Popup
  
# The Label widget is for rendering text. 
from kivy.uix.label import Label
  
# to change the kivy default settings we use this module config
from kivy.config import Config
  
# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', True)
  
# Make an app by deriving from the kivy provided app class
class PopupExample(App):
    # override the build method and return the root widget of this App
  
    def build(self):
        # Define a grid layout for this App
        self.layout = GridLayout(cols = 1, padding = 10)
  
  
        # Add a button
        self.button = Button(text ="Click for pop-up")
        self.layout.add_widget(self.button)
  
        # Attach a callback for the button press event
        self.button.bind(on_press = self.onButtonPress)
          
        return self.layout
  
    # On button press - Create a popup dialog with a label and a close button
    def onButtonPress(self, button):
          
        layout = GridLayout(cols = 1, padding = 10)
  
        popupLabel = Label(text = "Click for pop-up")
        closeButton = Button(text = "Close the pop-up")
  
        layout.add_widget(popupLabel)
        layout.add_widget(closeButton)       
  
        # Instantiate the modal popup and display
        popup = Popup(title ='Demo Popup',
                      content = layout)  
        popup.open()   
  
        # Attach close button press with popup.dismiss action
        closeButton.bind(on_press = popup.dismiss)   
  
# Run the app
if __name__ == '__main__':
    PopupExample().run()

chevron_right


Output:

When click on screen popup will open like this:

When click on Close the popup it will close.

 
Code #2:
In the second code when we use the size_hint and the size we can give the size accordingly. In this just add something as in the below code in line number 75.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Kivy example for the Popup widget
  
# Program to Show how to create a switch
# import kivy module   
import kivy 
     
# base Class of your App inherits from the App class.   
# app:always refers to the instance of your application  
from kivy.app import App
   
# this restrict the kivy version i.e 
# below this kivy version you cannot 
# use the app or software 
kivy.require('1.9.0')
  
# The Button is a Label with associated actions
# that is triggered when the button
# is pressed (or released after a click/touch).
from kivy.uix.button import Button
  
  
# The GridLayout arranges children in a matrix.
# It takes the available space and
# divides it into columns and rows,
# then adds widgets to the resulting “cells”.
from kivy.uix.gridlayout import GridLayout
  
# Popup widget is used to create popups.
# By default, the popup will cover
# the whole “parent” window.
# When you are creating a popup,
# you must at least set a Popup.title and Popup.content.
from kivy.uix.popup import Popup
  
# The Label widget is for rendering text. 
from kivy.uix.label import Label
  
# to change the kivy default settings we use this module config
from kivy.config import Config
  
# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', True)
  
# Make an app by deriving from the kivy provided app class
class PopupExample(App):
    # override the build method and return the root widget of this App
  
    def build(self):
        # Define a grid layout for this App
        self.layout = GridLayout(cols = 1, padding = 10)
  
  
        # Add a button
        self.button = Button(text ="Click for pop-up")
        self.layout.add_widget(self.button)
  
        # Attach a callback for the button press event
        self.button.bind(on_press = self.onButtonPress)
          
        return self.layout
  
    # On button press - Create a popup dialog with a label and a close button
    def onButtonPress(self, button):
          
        layout = GridLayout(cols = 1, padding = 10)
  
        popupLabel = Label(text = "Click for pop-up")
        closeButton = Button(text = "Close the pop-up")
  
        layout.add_widget(popupLabel)
        layout.add_widget(closeButton)       
  
        # Instantiate the modal popup and display
        popup = Popup(title ='Demo Popup',
                      content = layout,
                      size_hint =(None, None), size =(200, 200))  
        popup.open()   
  
        # Attach close button press with popup.dismiss action
        closeButton.bind(on_press = popup.dismiss)   
  
# Run the app
if __name__ == '__main__':
    PopupExample().run()

chevron_right


Output:
Popup size will be smaller than the window size.

Reference : https://kivy.org/doc/stable/api-kivy.uix.popup.html



My Personal Notes arrow_drop_up

Competative Programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.