Python | Bubble in kivy

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.

Bubble :

The Bubble widget is a form of menu or a small popup where the menu options are stacked either vertically or horizontally. The Bubble contains an arrow pointing in the direction you choose.

To choose the direction in which arrow points use:



Bubble(arrow_pos=’top_mid’)

The orientation of the bubble is by default horizontally but you can change it by the command:

orientation = ‘vertical’

To add items to the bubble:

bubble = Bubble(orientation = ‘vertical’)
bubble.add_widget(your_widget_instance)

To remove items:

bubble.remove_widget(widget)
or
bubble.clear_widgets()

Basic Approach :
1) import kivy
2) import kivyApp
3) import Button
4) import Floatlayout(according to need)
5) import Bubble
6) import object property
7) Create Layout class:
8) Create App class
9) create .kv file (name same as the app class):
        1) createBubble
10) return Layout/widget/Class(according to requirement)
11) Run an instance of the class

# Implementation of the Approach:

# .py code:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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'
  
# module consists the floatlayout  
# to work with FloatLayout first  
# you have to import it  
from kivy.uix.floatlayout import FloatLayout
  
# The Button is a Label with associated
# actions that are triggered
# when the button is pressed 
from kivy.uix.button import Button
  
# The Bubble widget is a form of menu or a
# small popup where the menu options
# are stacked either vertically or horizontally.
from kivy.uix.bubble import Bubble
  
# ObjectProperty is a specialized sub-class of the
# Property class, so it has the same
# initialisation parameters as it:
# By default, a Property always takes a default value[.]
from kivy.properties import ObjectProperty
   
  
# Create the Bubble class
# on which the .kv file is
class Cut_copy_paste(Bubble):
    pass
  
# Create the Layout Class
class BubbleDemo(FloatLayout):
   
    def __init__(self, **kwargs):
        super(BubbleDemo, self).__init__(**kwargs)
        self.but_bubble = Button(text ='Press to show bubble')
        self.but_bubble.bind(on_release = self.show_bubble)
        self.add_widget(self.but_bubble)
        self.bubb = Cut_copy_paste()
  
    # Defining the function to show the bubble
    def show_bubble(self, *l):
        self.add_widget(self.bubb)
              
# Create the App class
class BubbleApp(App):
    def build(self):
        return BubbleDemo()
  
# run the App
if __name__ == '__main__':
    BubbleApp().run()

chevron_right


.kv file:

filter_none

edit
close

play_arrow

link
brightness_4
code

# .kv file of the bubble
  
# Creating bubble
<Cut_copy_paste>:
    size_hint: (None, None)
    size: (160, 120)
    pos_hint: {'center_x': .5, 'y': .6}
    BubbleButton:
        text: 'Cut'
    BubbleButton:
        text: 'Copy'
    BubbleButton:
        text: 'Paste'

chevron_right


Output:



My Personal Notes arrow_drop_up

Competative Programmer

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.