Time delay Tkinter

2019-01-07 01:17发布

问题:

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')

回答1:

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()


回答2:

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.



回答3:

       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])