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.
Popup widget:
To use popup you must have to import :
from kivy.uix.popup import Popup
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.
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.
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, like:
BoxLayout: MyPopup: # bad ! |
Basic Approach:
1) import kivy
2) import kivyApp
3) import Widget
4) import Floatlayout
5) import Label
6) import popup
7) Set minimum version(optional)
8) Create widget class
9) Create Layout class :
10) create App class
9) create .kv file (name same as the app class):
1) create Widget
2) create popup
3) Give label to popup
4) create button to close popup
10) return Layout/widget/Class(according to requirement)
11) define popup function(Which shows the popup on press the button)
12) Run an instance of the class
Implementation of the Approach:
popup.py file
# 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') # Widgets are elements of a graphical user # interface that form part of the User Experience. from kivy.uix.widget import Widget # The Label widget is for rendering text. from kivy.uix.label import Label # module consist the floatlayout # to work with FloatLayout first # you have to import it from kivy.uix.floatlayout import FloatLayout # 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 # Creating a widget class # through this we add button # the commands of the class is in .kv file class Widgets(Widget): def btn(self): # calling of the show popup function show_popup() # Popup class is defined # The command of the class is in .kv file class Popups(FloatLayout): pass # create App class class MyApp(App): def build(self): # return the widget return Widgets() # define popup function in this we create the popup def show_popup(): show = Popups() popupWindow = Popup(title ="Popup Window", content = show, size_hint =(None, None), size =(200, 200)) # open popup window popupWindow.open() # Attach close button press with popup.dismiss action # content.bind(on_press = popup.dismiss) # run the App if __name__ == "__main__": MyApp().run() |
.kv file implementation:
# .kv file of the popup code # Adding Button widget <Widgets>: Button: text: "Press me" on_release: root.btn() # Adding Label, Button to popup <Popups>: Label: text: "You pressed the button" size_hint: 0.6, 0.2 pos_hint: {"x":0.2, "top":1} Button: text: "Close the popup" # set size of the button size_hint: 1, 0.4 # set position of the button pos_hint: {"x":0, "y":0.1} |
Output:
When button press popup appears, click anywhere in the window except popup screen popup will dissappear:

Recommended Posts:
- Python | Popup widget in Kivy
- Python | Progressbar widget in kivy using .kv file
- Python | Carousel Widget In Kivy using .kv file
- Python | Switch widget in Kivy using .kv file
- Python | Spinner widget in Kivy using .kv file
- Python | Carousel Widget In Kivy
- Python | Spinner widget in kivy
- Python | Progress Bar widget in kivy
- Python | Scrollview widget in kivy
- Python | BoxLayout widget in Kivy
- Python | Textinput widget in kivy
- Python | Switch widget in Kivy
- Python | Add image widget in Kivy
- Python | Slider widget in Kivy
- Python | Checkbox widget in Kivy
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.



