Change language

Python | Popup widget in Kivy using .kv file

| |

Kivy — it is a platform independent GUI tool in Python. Since it can run on Android, IOS, Linux, Windows, etc. It is mainly used to develop Android application, but that does not mean that it cannot be used in desktop applications.

Popup widget:

To use the popup, you must import:

 from kivy.uix.popup import Popup 

The Popup widget is used to create popups. By default, the pop-up window covers the entire "parent" window. When you create a popup, you must at least set Popup.title and Popup.content. 
Keep in mind that the default widget size is — size_hint = (1, 1). If you don’t want your popup to display full screen, you must either provide size hints with values ​​less than 1 (for example, size_hint = (. 8, .8)), or deactivate size_hint and use fixed size attributes.

Note: Popup — this is a special widget. Don’t try to add it as a child to any other widget. If you do this, the Popup will be treated like a normal widget and will not be hidden in the background, for example:

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 approach:

popup.py file

# Kivy example for Popup widget

 
# Program to show how to create a radio button
# kivy module import

import kivy 

 
# Your application base class inherits from the application class.
# app: always refers to your application instance

from kivy .app import App

 
# this limits the kivy version ie
# below this version you cannot
# use the application or software

kivy.require ( ’1.9.0’ )

 
# Widgets are graphical user elements
# an interface that is part of the user experience.

from kivy.uix.widget import Widget

# The "Label" widget is intended to render text.

from kivy.uix.label import Label

  
# the module consists of floatlayout
# work with FloatLayout first
# you must import it

from kivy.uix.floatlayout import FloatLayout

 
# The float widget is used to create popups .
# By default, the popup will span
# the entire parent window.
# When you create the popup,
# You must at least have Popup.title and Popup.content installed.

from kivy.uix.popup import Popup

 

  
# Create a widget class
# through this we add a button
# class commands are in the .kv file

class Widgets (Widget):

def btn ( self ):

  # call popup function show

  show_popup ()

 
# Popup class defined
# The class command is in the .kv file

class Popups (FloatLayout):

  pass

  
# create application class

class MyApp (App):

def build ( self ):

  # return widget

return Widgets ()

 
# define the 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

  popupWindow. open ()

 

# Attach a close button click with a popup action. dismiss

# content.bind (on_press = popup.dismiss)

 

 
# run application

if __ name__ = = "__ main__" :

MyApp (). run ()

# .kv popup code file

 
# Adding a widget button
"Widgets & gt ;:

Button:

text: "Press me"

on_release: root.btn ()

 
# Add label, popup button
"Popups & gt ;:

 

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 button size

size_hint: 1 , 0.4

# set button position

  pos_hint: { "x" : 0 , "y" : 0.1 }

Output:

When the button click popup appears, click anywhere in the window except the popup disappears:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically