The Wayback Machine - https://web.archive.org/web/20241118135915/https://www.geeksforgeeks.org/python-popup-widget-in-kivy/
Open In App

Python | Popup widget in Kivy

Last Updated : 06 Feb, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

👉🏽 Kivy Tutorial – Learn Kivy with Examples.

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.




# 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()


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.




# 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()


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

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



Previous Article
Next Article

Similar Reads

Python | Popup widget in Kivy using .kv file
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. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. Popup widget: To use popup you must have to import :
4 min read
Python | Scrollview 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 Desktop applications. Kivy Tutorial - Learn Kivy with Examples. Scroll view: The ScrollView widget provides a scrollable/pannable viewp
3 min read
Python | Carousel 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. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. Carousel widget: The Carousel widget provides the cla
3 min read
Python | BoxLayout 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. ???????? Kivy Tutorial - Learn Kivy with Examples. Now in this article, we will learn about the use of BoxLayout
5 min read
Python | Slider 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. ???????? Kivy Tutorial - Learn Kivy with Examples. Slider: To work with the slider you first have to import the mo
4 min read
Python | Checkbox widget in Kivy
Kivy is a platform independent GUI tool in Python. Kivy applications 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 Desktop applications. Kivy Tutorial - Learn Kivy with Examples. Checkbox widget -CheckBox is a specific two-state button th
4 min read
Python | Add image 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 Desktop applications. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. Image Widget: The Image widget is used to display an
4 min read
Python | Switch 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. ???????? Kivy Tutorial - Learn Kivy with Examples. Switch widget: The Switch widget is active or inactive, as a me
5 min read
Python | Switch widget in Kivy using .kv file
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. ???????? Kivy Tutorial - Learn Kivy with Examples. Switch widget: The Switch widget is active or inactive, as a me
4 min read
Python | Spinner widget in Kivy using .kv file
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. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. Spinner widget : To work with spinner you must have t
3 min read
Python | Spinner 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. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. Spinner widget : To work with spinner you must have t
5 min read
Python | Carousel Widget In Kivy using .kv file
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. ???????? Kivy Tutorial - Learn Kivy with Examples. Carousel widget: The Carousel widget provides the classic mobi
3 min read
Python | Progress Bar 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. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. Progressbar Widget: ProgressBar widget is used to vis
4 min read
Python | Progressbar widget in kivy using .kv file
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. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. Progressbar Widget: ProgressBar widget is used to vis
3 min read
Python | Textinput 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. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. TextInput: The TextInput widget provides a box for ed
3 min read
Python - Adding double tap on any widget in kivy
Kivy is a platform-independent GUI tool in Python. It can run on Android, IOS, Linux and Windows, etc. This is the only GUI library from python which can independently run on an android device even we can use it on Raspberry Pi also. It is an open-source Python library for the rapid development of applications that make use of innovative user inter
2 min read
How to add drag behavior in kivy widget ?
In this article, we will develop a GUI window using kivy framework of python, and we will add a Label having drag behavior on this window Note: You can follow the same pattern for implementing drag events on other widgets also. Approach: Actually what we are doing is we are using are just using the concept of inheritance, and we are making a new wi
2 min read
Python | Kivy .kv File
As when we write the application in Python kivy, to write all the things on the same code make a mess in the code and it is hard to understand that by someone another. Also writing a large code makes hard to maintain the construction of the widget tree and explicit the declaration of bindings.The KV language, allows us to create own widget tree in
3 min read
Introduction to Kivy ; A Cross-platform Python Framework
Kivy is an open-source, cross-platform Python framework used for developing multi-touch applications with a natural user interface. It allows developers to build applications that run on multiple platforms, including Windows, macOS, Linux, iOS, and Android. Kivy is based on the Model-View-Controller (MVC) architecture and uses a declarative languag
4 min read
Python | Ellipse (different polygons) 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. Kivy Tutorial - Learn Kivy with Examples. Ellipse: Ellipse is a vertex canvas instruction. It allows drawing of a
4 min read
Python | Float Layout 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. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. FloatLayout: Floatlayout allows us to place the eleme
5 min read
Python | Make a simple window using kivy
Kivy is a platform independent as it can be run on Android, IOS, linux and Windows etc. Kivy provides you the functionality to write the code for once and run it on different platforms. It is basically used to develop the Android application, but it Does not mean that it can not be used on Desktops applications. Use this command To install kivy: pi
5 min read
GridLayouts in Kivy | Python
Kivy is a platform independent as it can be run on Android, IOS, Linux and Windows, etc. Kivy provides you the functionality to write the code for once and run it on different platforms. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktop applications. Use this command To install kivy: pi
3 min read
Python | Add Label to a kivy window
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 Desktop applications.Label widget - The Label widget is for rendering text. It supports ASCII and unicode strings. The label is the text
4 min read
Python | Button Action 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 Desktop applications. ???????? Kivy Tutorial - Learn Kivy with Examples. Now in this article, we will learn how to build a button in kiv
3 min read
Python | Window size Adjustment in Kivy
Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. Kivy provides you the functionality to write the code for once and run it on different platforms. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.Kivy is that plat
4 min read
Python | Multiple Sliders widgets Controlling Background Screen or WindowColor in Kivy
Prerequisite - Slider in KivyKivy 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.In this article, we will learn How we can control the background color in kivy that me
3 min read
Python | AnchorLayout in Kivy using .kv file
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. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. AnchorLayout: The AnchorLayout aligns its children to
4 min read
Python| AnchorLayout 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. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. AnchorLayout: The AnchorLayout aligns its children to
3 min read
Python | StackLayout 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. ???????? Kivy Tutorial - Learn Kivy with Examples. StackLayout: To use StackLayout first import StackLayout by bel
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg