简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM