Reputation: 473
I have following code
main.py
class ExampleRoot(BoxLayout):
def any(self,*args):
x=0
while x<10:
server.sendmail(c,g,e)
total_emails="activity done"
#### progressbar not updating in live time
self.ids["pb"].value+=1
from jnius import autoclass
PythonActivity = autoclass('org.renpy.android.PythonActivity')
PythonActivity.toastError(total_emails)
x+=1
this is my kv file
<ExampleRoot>:
ProgressBar:
id:pb
max:10
value:0
progress bar is not updating in real time...when loop ends it suddenly increases progress bar....
is it possible to update scrollbar in realtime using thread?
Upvotes: 2
Views: 2144
Reputation: 8213
The reason your progressbar does not update until the end is because you are tieing up the MainThread with your loop. All Kivy GUI updates are done from the main thread, so if you block it, nothing in the GUI will update.
You can solve this two ways. If the content of your loop executes fast, you can just use Kivy's Clock object to run your function instead of looping:
def any(self, x, *args):
server.sendmail(c,g,e)
total_emails="activity done"
self.ids["pb"].value+=1
from jnius import autoclass
PythonActivity = autoclass('org.renpy.android.PythonActivity')
PythonActivity.toastError(total_emails)
x+=1
if x < 10:
Clock.schedule_once(lambda dt: self.any(x))
If your "sendmail" takes a while, it will still freeze the GUI while it is working. You can put this in a background thread, but you must remember to not modify GUI elements from a background thread, as GUIs are not threadsafe. You can again use Clock.schedule_once
to schedule a function that updates the progress bar.
Upvotes: 4