Search code examples
pythontkinterbackgroundconfigure

Python/Tkinter background not changing colours when configured to


I am trying to create a harmless prank joke on my friends, and I want the background of a python tkinter window(not canvas) to change to a random colour every second, then, after ten rounds, it will destroy itself. The problem is that when root.config(background=random_colour)is called, it will not change it's background colour. The entire code is below:

from tkinter import *
import pyglet
import time
import random

root = Tk()
text = Label( padx = 1000, pady = 999, text = 'VIRUS!' )
text.pack()
text.config(font=('Courier', 44))
root.attributes("-fullscreen", True)
root.update()

I'm cutting this bit out because it's just a list of all the named colours in python(It's called COLOURS).

for x in range(0, 9):
    colours_length = len(COLOURS)
    number = random.randint(0, colours_length)
    random_colour = COLOURS[number]
    root.config(background=random_colour)
    time.sleep(1)
    root.update()
root.destroy()

Solution

  • I've took acw1668's advice from the comments section, and it works now. Turns out that the label was covering the entire root window, and that was why it wasn't working.