There was a problem with the sleep () function in the Python programming language. Suppose I create a virtual pet program and I need to define a sleep function for it, its name is sleep_f . For this, I program the following actions:
Identification of the necessary objects -
Frame(this is just a black background) andButton(button 'Back')Deleting unwanted objects -
SleepbuttonFrameappearanceDelay
The appearance of the 'Back' button
A sample code is shown below:
from tkinter import * import time import random root = Tk() root.geometry('300x300') sleep = 50 button = Button(text = 'Sleep', command = lambda: sleep_f()) button.place(x = 0, y = 0) def sleep_f(): global sleep if sleep < 51: sleep_ = Frame(bg = 'black', width = 300, height = 300) back = Button(text = 'Back') button.place_forget() sleep_.place(x = 0, y = 0) time.sleep(random.randint(5, 15)) sleep = 100 back.place(x = 25, y = 200) root.mainloop() And everything seems to be fine, but in fact it is not. It turned out that when I press the Sleep button, which calls the function, the button sticks and the delay starts, and only after it appears the background and the 'Back' button. And I need it so that the background appears first, the delay starts, and after that the 'Back' button appears. I hope it is clear. Help me please.
UPD: I tried the method with root.after() , also unsuccessfully.
from tkinter import * import time import random root = Tk() root.geometry('300x300') sleep = 50 button = Button(text = 'Sleep', command = lambda: sleep_f()) button.place(x = 0, y = 0) def sleep_f(): global sleep if sleep < 51: sleep_ = Frame(bg = 'black', width = 300, height = 300) back = Button(text = 'Back') button.place_forget() sleep_.place(x = 0, y = 0) sleep = 100 root.after(random.randint(5000, 15000), back.place(x = 25, y = 200)) root.mainloop() What am I doing wrong?
UPD: Issue resolved
aftermethod takes a reference to a function, and you pass it aNone, returned from a call to theplacemethod. - Sergey Gornostaev