Python | Popup widget in Kivy - GeeksforGeeks

06 Feb, 2020
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.
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
# 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 configfrom kivy.config import Config  # 0 being off 1 being on as in true / false# you can use 0 or 1 && True or FalseConfig.set('graphics', 'resizable', True)  # Make an app by deriving from the kivy provided app classclass 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 appif __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 configfrom kivy.config import Config  # 0 being off 1 being on as in true / false# you can use 0 or 1 && True or FalseConfig.set('graphics', 'resizable', True)  # Make an app by deriving from the kivy provided app classclass 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 appif __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
Python-gui
Python-kivy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()