Table of contents
1.
Introduction
2.
Popup Widget
3.
Code 1
3.1.
Output
4.
Code 2
4.1.
Output
5.
FAQs
5.1.
What is a widget in Python Kivy?
5.2.
What is the modal view in Kivy?
5.3.
What is the difference between GridLayout and stacklayout?
5.4.
What is the relative layout in Kivy?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Popup Widget in Kivy

Author SHIVANGI MALL
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Kivy is a multi-platform Python GUI development library that runs on iOS, Android, Windows, OS X, and GNU/Linux. It aids in the development of apps that make use of cutting-edge multi-touch user interfaces. Kivy's core concept allows developers to create an app once and deploy it across all devices, making code reusable and deployable and enabling quick and easy interaction design and prototyping.

Popup Widget

Modal popups are created with the Popup widget. The popup will cover the entire "parent" window by default. Popup.title and Popup.content must be set at the very least when generating a popup.

  • A popup window is a window that appears unexpectedly on the screen. In most cases, a popup window occurs when a person visits a page or exits a window.
  • When we need to transmit certain obvious messages to the user, we employ popup dialogues. Popup dialogues can still be used to send messages to the user through status bars, as well as for specific messages that need to be told with emphasis.
  • One thing to keep in mind is that a widget's default size is size_hint=. (1, 1).
  • If you don't want your popup to take up the entire screen, use size hints with values smaller than 1 (for example, size_hint=(.7,.7)) or disable the size_hint and use fixed-size properties instead.

 

To use popup, you must have to import :

import kivy  
from kivy.app import App  
from kivy.uix.button import Button  
from kivy.uix.label import Label  
from kivy.uix.popup import Popup  
from kivy.uix.gridlayout import GridLayout 
You can also try this code with Online Python Compiler
Run Code

Code 1

Using the code below, we can generate a popup window that will cover the entire "parent" window in Kivy.

# to import kivy module
import kivy  
from kivy.app import App  
from kivy.uix.button import Button  
from kivy.uix.label import Label  
# we use Popup widget to create popups.
from kivy.uix.popup import Popup  
# GridLayout arranges children in a matrix.
from kivy.uix.gridlayout import GridLayout  
 
class Popup_Example(App):  
 
    def build(self):  
         # to define a grid layout for this App
        layout = GridLayout(cols = 1, padding = 10)  
        # Add a button
        self.button = Button(text="Click Here to view Popup",  
                             size_hint = (0.8, 0.2),  
                             pos_hint = {"x":0.1, "y":0.1})  
 
        layout.add_widget(self.button)  
 
        self.button.bind(on_press = self.onButtonPress)  
 
        return layout  
 
    def onButtonPress(self, button):  
 
        layout      = GridLayout(cols = 1, padding = 10)  
 
        popupLabel  = Label(text  = "Welcome User")  
 
        closeButton = Button(text = "Close the pop-up window")  
 
        layout.add_widget(popupLabel)  
 
        layout.add_widget(closeButton)        
 
        popup = Popup(title = 'Demo Popup',  
 
                      content = layout)    
 
                   
 
        popup.open()    
 
        closeButton.bind(on_press = popup.dismiss)    
 
root = Popup_Example()  
# Run the app
root.run()  
You can also try this code with Online Python Compiler
Run Code

Output


 

Code 2

When we use the size hint and size in the second code, so we can give the size accordingly.

# to import kivy module
import kivy  
from kivy.app import App  
from kivy.uix.button import Button  
from kivy.uix.label import Label  
# We use Popup widget to create popups.
from kivy.uix.popup import Popup  
# GridLayout arranges children in a matrix.
from kivy.uix.gridlayout import GridLayout  
 
class Popup_Example(App):  
 
    def build(self):  
         # to define a grid layout for this App
        layout = GridLayout(cols = 1, padding = 10)  
        # Add a button
        self.button = Button(text="Click Here to view Popup",  
                             size_hint = (0.8, 0.2),  
                             pos_hint = {"x":0.1, "y":0.1})  
 
        layout.add_widget(self.button)  
 
        self.button.bind(on_press = self.onButtonPress)  
 
        return layout  
 
    def onButtonPress(self, button):  
 
        layout      = GridLayout(cols = 1, padding = 10)  
 
        popupLabel  = Label(text  = "Welcome User")  
 
        closeButton = Button(text = "Close the pop-up window")  
 
        layout.add_widget(popupLabel)  
 
        layout.add_widget(closeButton)        
 
        popup = Popup(title = 'Demo Popup',  
 
                      content = layout, size_hint =(None, None), size =(200, 200))    
 
                   
 
        popup.open()    
 
        closeButton.bind(on_press = popup.dismiss)    
 
root = Popup_Example()  
# Run the app
root.run()  
You can also try this code with Online Python Compiler
Run Code

Output

FAQs

What is a widget in Python Kivy?

In Kivy, a Widget is the fundamental component of a GUI interface. It comes with a Canvas that you may use to draw on the screen. It receives and responds to events.

Kivy's widgets are arranged in a tree. Your program has a root widget, which normally has children who can have their own children. The children attribute, a Kivy ListProperty, is used to represent the children of a widget.

 

What is the modal view in Kivy?

Modal views are created with the ModalView widget. The view will by default cover the whole "primary" window.

Remember that a Widget's default size is size hint=. (1, 1). We use size_hints with values lower than 1 (for example, size_hint=(.8,.8)) or disable the size_hint and use fixed-size attributes if you don't want your view to be fullscreen.

 

What is the difference between GridLayout and stacklayout?

GridLayout: Grid Layout creates a grid of widgets. To determine the size of the elements and how to arrange them, you must supply at least one grid dimension.

StackLayout: StackLayout places widgets next to each other, but with a fixed size in one of the dimensions, rather than attempting to fit them all into the available space.

 

What is the relative layout in Kivy?

We can use relative layout to set relative coordinates for children. Use the FloatLayout if you want absolute placement. The RelativeLayout class is similar to the FloatLayout class, except that its child widgets are positioned relative to the layout. 

This layout works similarly to FloatLayout, with the exception that the positioning properties (pos, x, center x, right, y, center y, and top) are related to the Layout size rather than the window size.

Conclusion

In this article, we have extensively discussed popup widget in kivy.

We hope that this blog has aided you in broadening your horizons. If you want to learn more, check out our articles on Application DevelopmentModules & Packages in Python, and Best Python Projects. Do upvote our blog to help other ninjas grow.
Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!
Happy Reading!

Live masterclass