Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

python - Time.sleep()影响python中上面的代码(Time.sleep() affecting the code above it in python)

I am making a KIVY program in python and have a time.sleep(3) in my code so that it waits three seconds before changing the screen.

(我正在用python创建一个KIVY程序,并且在我的代码中有一个time.sleep(3),以便它在更改屏幕之前等待三秒钟。)

But the function above it works after the 3 seconds and not before it.

(但是它上方的功能会在3秒钟后而不是之前起作用。)

I am having no errors and I have tried everything but nothing seems to work.

(我没有错误,我已经尝试了一切,但似乎没有任何效果。)

Here is the snippet.

(这是代码段。)

def input_button(self, instance): # creating the button that when pressed updates the label
    query = "You Said {}".format(self.command()) # making the query
    if query == "You Said None":
        self.update_info('Please input a command')
    else:
        self.update_info(query) # updating the label
        time.sleep(3)
        pa_app.screen_manager.current = "Result"

The self.update_info(query) runs after three seconds but the time.sleep is after it.

(self.update_info(query)在三秒钟后运行,但是time.sleep在它之后。)

  ask by Apurv Pandey translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Kivy is a GUI framework and you cannot add sleep statements safely.

(Kivy是一个GUI框架,您不能安全地添加sleep语句。)

when

(什么时候)

self.update_info(query)

is called kivy will update the GUI only after the last line of your function is called, because this is when you give back the control to the Kivy gui engine.

(所谓的kivy仅在调用函数的最后一行之后才会更新GUI,因为这是将控件交还给Kivy gui引擎的时候。)

You have to check whether kivy does have timers.

(您必须检查kivy是否具有计时器。)

You can start a timer and tell it to call another function that does the

(您可以启动一个计时器,并告诉它调用执行该操作的另一个函数。)

pa_app.screen_manager.current = "Result"

whenever the timer finished.

(每当计时器结束时。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...