Skip to content
Related Articles

Related Articles

Improve Article

Python | Progress Bar widget in kivy

  • Last Updated : 20 Oct, 2020

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.

👉🏽 Kivy Tutorial – Learn Kivy with Examples.

Progressbar Widget:

ProgressBar widget is used to visualize the progress of some task. Only the horizontal mode is currently supported: the vertical mode is not yet available.
The progress bar has no interactive elements and is a display-only widget.

To use it, simply assign a value to indicate the current progress:




from kivy.uix.progressbar import ProgressBar
pb = ProgressBar(max = 1000)
 
# this will update the graphics automatically (75 % done)
pb.value = 750

To work with the progressbar widget you must have to import it by the command:



from kivy.uix.progressbar import ProgressBar

The progeress bar take takes two arguments:

1) max: Maximum value allowed for value. It is a Numericproperty and defaults to 100.
2) value: Current Value for the slider

Basic Approach:

1) import kivy
2) import kivyApp
3) import progressbar
4) import Boxlayout(according to need)
5) Set minimum version(optional)
6) Create Layout class:
7) Create App class
8) return Layout/widget/Class(according to requirement)
9) Run an instance of the class

Note: This code is for a complete working progressbar and you need to focus only the creation and working of the progressbar no need to take stress on other properties I will be covering them in future.
You first have to click on the button that is made in the file and then the progressbar will show.

Implementation of the Approach:




# Program to Show how to create a Progressbar in .kv file
 
# 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')
   
# The ProgressBar widget is used to
# visualize the progress of some task
from kivy.uix.progressbar import ProgressBar
 
# BoxLayout arranges children in a vertical or horizontal box.
# or help to put the children at the desired location.
from kivy.uix.boxlayout import BoxLayout
 
# The Clock object allows you to schedule a
# function call in the future
from kivy.clock import Clock
  
# The Button is a Label with associated actions
# that is triggered when the button
# is pressed (or released after a click / touch). 
from kivy.uix.button import Button
 
# 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
 
# A Widget is the base building block
# of GUI interfaces in Kivy.
# It provides a Canvas that
# can be used to draw on screen.
from kivy.uix.widget import Widget
 
# ObjectProperty is a specialised sub-class
# of the Property class, so it has the same
# initialisation parameters as it:
# By default, a Property always takes a default
# value[.] The default value must be a value
# that agrees with the Property type.
from kivy.properties import ObjectProperty
 
 
 
# Create the widget class
class MyWidget(Widget):
 
    progress_bar = ObjectProperty()
     
    def __init__(self, **kwa):
        super(MyWidget, self).__init__(**kwa)
         
        self.progress_bar = ProgressBar()
        self.popup = Popup(
            title ='Download',
            content = self.progress_bar
        )
        self.popup.bind(on_open = self.puopen)
        self.add_widget(Button(text ='Download', on_release = self.pop))
 
    # the function which works when you clicj = k the button
    def pop(self, instance):
        self.progress_bar.value = 1
        self.popup.open()
 
    # To continuesly increasing the value of pb.
    def next(self, dt):
        if self.progress_bar.value>= 100:
            return False
        self.progress_bar.value += 1
     
    def puopen(self, instance):
        Clock.schedule_interval(self.next, 1 / 25)
 
# Create the App class
class MyApp(App):
    def build(self):
        return MyWidget()
 
# run the App
if __name__ in ("__main__"):
    MyApp().run()

Output:

Image 1:

Image 2:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :