yasath
yasath

Reputation: 59

Repeatedly change Tkinter window colour to generated colour

I have Python code to generate the average colour of the screen as an RGB value and a hex code. The code repeats itself through a while True loop, and I want to put the instructions to change the window colour at the end of this loop.

I have this code at the moment:

from Tkinter import *
from colour import Color

root = Tk()
root.configure(background="grey")
root.geometry("400x400")
root.mainloop()
while True:
    [ COLOUR GENERATING SCRIPT ]
    hexcolour = Color(rgb=(red, green, blue))
    root.configure(background=hexcolour)

Can someone please tell me how I can initiate a Tkinter window and then change the colour each time the loop runs?

I'm running Python 2.7 for this project.

Upvotes: 0

Views: 802

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386210

You need to remove the while loop entirely. Instead, create a function that is what you would do in the loop, and then have that function call itself via after. It will then run for the life of the program.

from Tkinter import *
from colour import Color

def changeColor():
    [ COLOUR GENERATING SCRIPT ]
    hexcolour = Color(rgb=(red, green, blue))
    root.configure(background=hexcolour)

    # call this function again in one second
    root.after(1000, changeColor)

root = Tk()
root.configure(background="grey")
root.geometry("400x400")

# call it once, it will run forever
changeColor()

root.mainloop()

Upvotes: 2

Related Questions