0

I want to change the background color over time (E.g. red-> 0.5 seconds later-> blue,like animation)

I tried like this. but didn't I wanted, and it immediately came out black.

I put the main loop in front, but it didn't work until. what should I do?

import tkinter

window=tkinter.Tk()
window.title("COLOR")
window.geometry("640x400+100+100")
window.resizable(False, False)

window.configure(bg="red")
window.after(50)
window.configure(bg="blue")
window.after(50)
window.configure(bg="green")
window.after(50)
window.configure(bg="black")
window.after(50)

window.mainloop()
kimhanuu
  • 165
  • 1
  • 1
  • 11
  • 1
    Here's an example of how to configure a repeating timer: https://stackoverflow.com/questions/2400262/how-to-create-a-timer-using-tkinter – Aaron Jan 17 '20 at 15:06
  • You can schedule functions to be called after a specific delay in tkinter by using the universal [`after()`](https://web.archive.org/web/20190222214221id_/http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html) widget method. There are many answers here showing how to use if you search for them. – martineau Jan 17 '20 at 15:07
  • Basically, with your code; nothing will happen until you call window.mainloop(), and you don't give `window.after()` a function to call after 50ms is up, so nothing will happen when each of your 4 timers expire (at the same time) – Aaron Jan 17 '20 at 15:07

2 Answers2

0

You can use after() to schedule task to change color:

import tkinter as tk

window = tk.Tk()
window.title('COLOR')
window.geometry('640x480+100+100')
window.resizable(0, 0)

def change_color(i=0):
    if i < 4:
        colors = ('red', 'blue', 'green', 'black')
        window.config(bg=colors[i])
        window.after(500, change_color, i+1)

change_color()
window.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • `window.config(bg=_from_rgb(tuple(colors))) window.after(500,change_color)` I did this but it didn't work (the color comes as a list: ex: [255,0,255]) _from_rgb () is a function that converts rgb to tkinter. ) – kimhanuu Jan 17 '20 at 16:01
  • In `window.config(bg=_from_rgb(tuple(colors)))`, is `colors` a single list like `[255,0,255]`, or the whole list of colors like `[[255,0,255],[...],...]`? If it is the latter one, you should use `colors[i]`. Also, you missed an argument in `window.after(500, change_color)`, it should be `window.after(500, change_color, i+1)`. – acw1668 Jan 18 '20 at 00:03
  • It is [255,0,255] – kimhanuu Jan 19 '20 at 15:23
  • Then I want to know what `it didn't work` means. It did not show the correct color or it did not change color every half second? What is the implementation of `_from_rgb()` function? – acw1668 Jan 19 '20 at 15:59
0

You should use a function called via the after() method to change the color and then in that function, schedule another call. The following shows one way. Rather that doing something, then delay, then do something else we start the mainloop and then after a delay we do something and setup another delayed call.

import sys
import tkinter as tk

def change(delay, frame, sequence, index):
    index = (index + 1) % len(sequence)
    frame.configure(background=sequence[index])
    frame.after(delay, lambda: change(delay, frame, sequence, index))

def main(argv=None):
    sequence = ['black', 'grey40', 'grey60', 'grey80', 'white', 'grey80', 'grey60', 'grey40']
    root = tk.Tk()
    frame = tk.Frame(root, width=200, height=200, background="red")
    frame.pack(fill=tk.BOTH, expand=True)
    change(100, frame, sequence, -1)
    root.mainloop()
    return 0

if __name__ == '__main__':
    sys.exit(main(sys.argv))
patthoyts
  • 32,320
  • 3
  • 62
  • 93