8

I'd like to make a graphic window in PyDev (Eclipse) python 2.75.

I've done few things but I'd like to make an "entrance" "blink". It's Tests the user input. If it's an integer it should blink green for a second, and then turn into white. But if it's a string of something else it should blink red, and then turn into white. I've used a time.sleep() but it doesn't work as I'd like to.

Here is my code for this action:

def sprawdzam():

    z = e.get()
    try:
        z = int(z)
        e.config(bg = 'green')
        time.sleep(2)
        e.config(bg = 'white')    

    except:
        l.config(bg = 'red')
        time.sleep(2)
        e.config(bg = 'white')
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Maq92
  • 295
  • 1
  • 4
  • 16

3 Answers3

13

time.sleep blocks the execution of the program.

Use after.

For example:

from Tkinter import *

def blink():
    e.config(bg='green')
    e.after(1000, lambda: e.config(bg='white')) # after 1000ms

root = Tk()
e = Entry(root)
e.pack()
b = Button(root, text='blink', command=blink)
b.pack()
root.mainloop()
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • @Maq92, Please post another separated question. – falsetru Nov 10 '13 at 09:05
  • Is there any way to have it wait for exactly one execution of the mainloop? I just want to wait for a widget to refresh its display before running the rest. – ArtOfWarfare Jan 15 '16 at 14:32
  • @ArtOfWarfare, Sorry. I don't understand what do you mean. Could you post a question with an example code? – falsetru Jan 15 '16 at 14:59
  • This answer allows you to delay by a fixed amount of time. `Tk` has a runloop, which you start by calling `root.mainloop()`. Is there any way to, instead of waiting a fixed amount of time, just have `Tk` wait until it completes one run of its mainloop, whether it takes a fraction of an ms or it takes 5000 ms? – ArtOfWarfare Jan 15 '16 at 15:38
  • @ArtOfWarfare, How about putting code you want to run after `root.mainloop()` call? – falsetru Jan 15 '16 at 16:29
  • After mainloop exits the entire program should be done. Every run of the loop has finished at that point. I want to wait for the current run of the loop to finish, not every run of the loop to finish. – ArtOfWarfare Jan 15 '16 at 16:30
0

First of all, You should not use try/except blocks to manage your code. Second of all, you are using e.config and l.config to switch your colors, which one is it supposed to be? (You should consider better naming conventions for your variables and objects to reduce confusion).

You can check the type of object the user input and then have a better managed flow like so:

def sprawdzam():
    content = e.get()
    if content.isalnum():
        e.config(bg = 'green')    
    else:
        e.config(bg = 'red')
    time.sleep(2)
    e.config(bg = 'white')

Here I used e as the object to change colors on, but this might not be true for your code, you should make sure you are doing it with the right objects.

As you can see, the sleep and the switch to white is done outside the if/else because no matter what, you will wait then turn to white, no need to write that code twice.

Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
0
       for P in range(len(MaxTrace)):
           T = P + 1
           if T < len(MaxTrace):
               PrevPlate  , PrevDot   = MaxTrace[P][0], MaxTrace[P][1] 
               TargetPlate, TargetDot = MaxTrace[T][0], MaxTrace[T][1]
               self.__window.update()
               sleep(0.3)
               #replace REGULAR token img to ACTIVE token img
               GameCanvas.itemconfig(self.tokens[PrevPlate,PrevDot],image=self.DotImgActv[RivalColor])
               self.__window.update()
               sleep(0.2)
               # mark Target vacation for move to by Yellow-RING img
               self.tokens[TargetPlate, TargetDot]=GameCanvas.create_image(DotXY[TargetPlate,TargetDot],
                                                                           image=self.DotVacantImg  )
               self.__window.update()
               sleep(0.4)
               GameCanvas.delete(self.tokens[PrevPlate,PrevDot])
               self.__window.update()
               sleep(0.3)
               GameCanvas.itemconfig(self.tokens[TargetPlate, TargetDot],image=self.DotImg[RivalColor])
Evgeny
  • 1
  • You can use root.update() , than sleep() works, and do the job better than .after(...). There is an example of a loop with delay on each step: – Evgeny Apr 01 '17 at 18:17