link
link

Reputation: 23

Time.sleep() not pausing the correct moment?

Hello I have created a quiz using python and tkinter. After each option is pressed I wanted the correct answer to turn green and the three incorrect to turn red then revert to the default for the next question.The problem here being that running the code will take the buttons to the default before the user can see the colours. To do this I tried to use time.sleep() in the function however no matter where I use it it just seems to pause on the button being pressed down and then goes onto the next question without seeing any colour change.

Here is the relevant piece of code

def entry(num):
   global score
   global x
   global count
   count +=1

   if Qa[x] == 1:
       option1.config(bg = "green")
       option2.config(bg = "red")
       option3.config(bg="red")
       option4.config(bg="red")
   elif Qa[x] == 2:
       option1.config(bg="red")
       option2.config(bg="green")
       option3.config(bg="red")
       option4.config(bg="red")

   elif Qa[x] == 3:
       option1.config(bg="red")
       option2.config(bg="red")
       option3.config(bg="green")
       option4.config(bg="red")
   elif Qa[x] == 4:
       option1.config(bg="red")
       option2.config(bg="red")
       option3.config(bg="red")
       option4.config(bg="green")
   if num == Qa[x]:
       score += 1
   x +=1
   if count <10:
       my_label.config(text = Qs[x])
       option1.config(text = (question_prompts[x])[1],bg = "SystemButtonFace",command = lambda: entry(1) )
       option2.config(text=(question_prompts[x])[2],bg = "SystemButtonFace",command = lambda: entry(2) )
       option3.config(text=(question_prompts[x])[3],bg = "SystemButtonFace",command = lambda: entry(3) )
       option4.config(text=(question_prompts[x])[4],bg = "SystemButtonFace",command = lambda: entry(4) )
   else:
       End_score =Label(text = "Well done you scored" +" "+ str(score)+" " +"out of 11", font = 40)
       End_score.place(relx=0.5,rely =0.5,anchor = CENTER)
   print(x,score, count, Qa[x])

I haven't put the time.sleep() in here because I have tried it everywhere in this section an it gives the same result I would really appreciate some help

Upvotes: 0

Views: 54

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54955

The PROBLEM here is that the options will not actually change color until Tk can get back to its main loop. As long as you are running your function, the main loop cannot pull new events. You need to set the colors, then use root.after to schedule a callback at some point in the future where you reset to all green.

Upvotes: 1

Related Questions