Apurv Pandey
Apurv Pandey

Reputation: 11

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. But the function above it works after the 3 seconds and not before it. 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.

Upvotes: 0

Views: 445

Answers (2)

Apurv Pandey
Apurv Pandey

Reputation: 11

I fixed this problem by using the from kivy.clock import as Clock module. I used Clock.schedule_once functions and passed self.change_screen, 10. I created the self.change_screen function as the Clock.schedule_once only takes a function and time as the parameters. Although I wanted to wait for 3 seconds, passing 3 in the function didn't make it wait for 3 seconds but less than it. So for having the same effect I passed in 10. This solved the problem.

Upvotes: 1

gelonida
gelonida

Reputation: 5630

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

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.

You have to check whether kivy does have timers. 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.

Upvotes: 0

Related Questions