for i in range(10):
grab_data_chunk
update_progressbar
for i in range(10):
grab_data_chunk()
update_progressbar()
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.progressbar import ProgressBar
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from time import sleep
class MainMenu(BoxLayout):
def __init__(self, **kwargs):
super(MainMenu, self).__init__(**kwargs)
self.orientation = 'vertical'
btn = Button(text="Start")
btn.bind(on_release=self.trigger)
self.add_widget(btn)
self.MyList = ('this', 'is', 'a', 'test')
self.i = 0
self.pb = ProgressBar(max = len(self.MyList), value = 0)
self.add_widget(self.pb)
def trigger(self, *args):
self.i = 0
self.pb.value = 0
Clock.schedule_interval(self.heavyFunc,0.1)
def heavyFunc(self, dt):
sleep(0.5)
print(self.MyList[self.i])
self.i += 1
self.pb.value +=1
if self.i >= len(self.MyList):
Clock.unschedule(self.heavyFunc)
print('unscheduled')
class TestApp(App):
def build(self):
return MainMenu()
if __name__ == "__main__":
TestApp().run()
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.progressbar import ProgressBar
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from time import sleep
class TaskSupplier(object):
"""
A placeholder object for the function that do the real work.
Not really required, but I like using class to keep namespaces clean ;-)
"""
@staticmethod
def do_task(callback):
""" Does the heavy lifting/processing. Calls 'callback' when done """
print "Processing task...."
sleep(0.5)
callback()
class ProgressAnimator(object):
"""
This class handles the animation of a 'ProgressBar' instance given a list
of functions that do the real work. Each of these functions need to accept
a single 'callback' parameter specifying the function to call when done.
"""
def __init__(self, progressbar, callbacks):
self.pb = progressbar # Reference to the progress bar to update
self.pb.max, self.pb.value = len(callbacks), 0
self.callbacks = callbacks # A list of callback functions doing work
self.index = 0 # The index of the callback being executed
Clock.schedule_once(lambda dt: callbacks[0](self.task_complete), 0.1)
def task_complete(self):
""" The last called heavy worker is done. See if we have any left """
self.index += 1
self.pb.value = self.index
if self.index < len(self.callbacks):
Clock.schedule_once(
lambda dt: self.callbacks[self.index](self.task_complete), 0.1)
class MainMenu(BoxLayout):
def __init__(self, **kwargs):
super(MainMenu, self).__init__(**kwargs)
self.orientation = 'vertical'
# We add 2 buttons to demonstrate the generic nature of the
# ProgressAnimator
btn = Button(text="Start 3")
btn.bind(on_release=lambda btn: self.trigger(3))
self.add_widget(btn)
btn = Button(text="Start 6")
btn.bind(on_release=lambda btn: self.trigger(6))
self.add_widget(btn)
self.pb = ProgressBar()
self.add_widget(self.pb)
def trigger(self, number):
""" Start the list of tasks """
# The tasklist can be any list of functions. The only requirement is
# that each accepts 1 parameter - callback to call when done
task_list = [TaskSupplier.do_task for num in xrange(0, number)]
ProgressAnimator(self.pb, task_list)
#!\usr\bin\env python
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.progressbar import ProgressBar
from kivy.garden import ProgressSpinner
from commands import getoutput
from subprocess import check_output
import ansiconv
Builder.load_string('''
<myPB>:
value: 400
max: 500
canvas:
Rectangle:
source: "kapy-bg.jpg"
size: self.size
pos: self.pos
<CoolWidget>:
orientation: 'vertical'
Button:
size_hint: 1, .2
text: 'top!'
on_press: root.getTop()
ProgressBar:
id: pb
max: 1000
value: 200
Label:
id: top_label
font_size: root.width / 50
#pos_hint: {'x': -.7, 'y': .3}
''')
class myPB(ProgressBar):
def __init__(self, **kwargs):
super(myPB, self).__init__(**kwargs)
print '\n\n TEST PB CREATED \n\n'
class Coolwidget(BoxLayout):
def __init__(self, **kwargs):
super(Coolwidget, self).__init__(**kwargs)
self.top_label = self.ids['top_label']
self.pb = self.ids['pb']
self.testpb = myPB
def getTop(self):
output = getoutput("top -p 1 -n 1 -b ")
#output = check_output([])
self.pb.value = 900
print ansiconv.to_plain(output)
output = ansiconv.to_plain(output) # Convert output so that its usable
self.add_widget(self.testpb(value=400))
#self.top_label.text = output.split()
class Coolapp(App):
def build(self):
return Coolwidget()
if __name__ == '__main__':
Coolapp().run()