deounix
deounix

Reputation: 141

Print and clear output on screen with Kivy

I'm trying to print some words on screen with kivy. What I'm looking for is print first word then sleep 2 seconds then clear first word and print second word

What I'm currently doing:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout

from random import choice
from time import sleep

xWords = ["hello1", "hello2", "hello3", "hello4", "hello5"]

class Test(GridLayout):
    def __init__(self, **kwargs):
        super(Test, self).__init__(**kwargs)
        self.cols = 1
        for x in xrange(2):
            # I want it to show frist word then sleep 2 sec then clear first word from screen then print second word
            self.add_widget(Label(text = "[b]"+choice(xWords)+"[/b]", markup = True, font_size = "40sp"))
            sleep(2)
        # then clear all words in screen
        for x in xrange(5):
            # then show the new 4 words
            self.add_widget(Label(text = "[b]"+choice(xWords)+"[/b]", markup = True, font_size = "40sp"))

class TestApp(App):
    def build(self):
        return Test()

if __name__ == "__main__":
    TestApp().run()

How can I do this?

Upvotes: 0

Views: 2372

Answers (1)

inclement
inclement

Reputation: 29450

Don't use time.sleep, this blocks the gui because the whole function doesn't return until time.sleep does.

Use Clock.schedule_once instead. Below is a simple example to call a function called update in 2 seconds, you can do whatever you want within that function including scheduling another one.

from kivy.clock import Clock
class Test(GridLayout):
    def __init__(self, **kwargs):
        super(Test, self).__init__(**kwargs)
        Clock.schedule_once(self.update, 2)  # 2 is for 2 seconds
    def update(self, *args):
        self.clear_widgets()
        # then add some more widgets here

Upvotes: 1

Related Questions