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 Development, Modules & 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!