我正在尝试使用kivy
在屏幕上打印一些字词。
我正在寻找的是打印第一个单词然后睡2秒然后清除第一个单词并打印第二个单词
我目前正在做的事情:
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()
我该怎么做?
答案 0 :(得分:1)
不要使用time.sleep
,这会阻止gui,因为整个函数在time.sleep
之前都不会返回。
请改用Clock.schedule_once
。下面是一个在2秒内调用一个名为update的函数的简单示例,您可以在该函数中执行任何操作,包括调度另一个函数。
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