Python ForLoop with nested after() functions happening after loop

后端 未结 2 1884
伪装坚强ぢ
伪装坚强ぢ 2020-12-21 06:29

I am trying to create a function that will repeat a block of code three times. The code has a for loop to alter the background at 500ms intervals. I want this to be repeated

2条回答
  •  清酒与你
    2020-12-21 06:59

    Fernando Matsumoto has answered your question, but here's a slightly more compact way to do it.

    import Tkinter as tk
    
    bgcolors = ("blue", "green", "yellow", "purple", "red", "#a1dbcd")
    
    def cycle():
        delta = 500
        delay = delta
        for x in range(3):
            for c in bgcolors:
                window.after(delay, lambda c=c: window.configure(bg=c))
                delay += delta
            print x
    
    window = tk.Tk()
    window.pack_propagate(0)
    
    b = tk.Button(window, text='cycle bg', command=cycle)
    b.pack()
    
    window.mainloop()
    

提交回复
热议问题