Last active
May 30, 2021 04:25
-
-
Save VICTORVICKIE/09b59931e8a0bb3644d9e055ee736fee to your computer and use it in GitHub Desktop.
Kivy Loading Widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kivy.properties import ObjectProperty, NumericProperty, BooleanProperty | |
from kivy.uix.floatlayout import FloatLayout | |
from kivy.uix.progressbar import ProgressBar | |
from threading import Thread | |
from time import sleep | |
class LoadingWidget(FloatLayout): | |
task = ObjectProperty() # -> task an object of the task to be performed on thread | |
task_thread = ObjectProperty(None) # -> Internal Helper object | |
anim = BooleanProperty(False) # -> Internal Helper Property | |
def on_task(self, *largs): | |
'''' on_task fires when an task is assigned to LoadingWidget ''' | |
# Widget stuffs | |
self.p = ProgressBar(size_hint=(.5, None), pos_hint= {'center_x': .5, 'center_y': .5}) | |
self.add_widget(self.p) | |
# Threading Stuffs | |
self.task_thread = Thread(target=self.task, daemon=True) | |
self.task_thread.start() | |
Thread(target=self.stop_spin, daemon=True).start() | |
Thread(target=self.loading_animation, daemon=True).start() | |
def stop_spin(self): | |
self.task_thread.join() | |
self.anim = False | |
self.p.value = 100 | |
sleep(0.5) | |
self.remove_widget(self.p) | |
def loading_animation(self,*largs): | |
self.anim = True | |
value = 0 | |
while self.anim: | |
if value >= 100: | |
value = 0 | |
else: | |
self.p.value = value | |
value += 1 | |
sleep(0.025) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Imports | |
from kivy.app import App | |
from kivy.lang import Builder | |
from time import sleep | |
from LoadManager import LoadingWidget #above file! | |
# Simple Window UI | |
KV = """ | |
FloatLayout: | |
LoadingWidget: | |
id:progress | |
""" | |
# App class | |
class ExampleApp(App): | |
def build(self): | |
return Builder.load_string(KV) | |
def on_start(self): | |
# pass on the task to be performed to the LoadingWidget done here using ids! | |
self.root.ids.progress.task = self.calculate | |
# Heavy function that need to be run on thread! | |
def calculate(self): | |
for i in range(1,20): | |
print(f"{20-i} seconds till end") | |
sleep(1) | |
ExampleApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment