Tk: Change Background Color Successively

Python 3.10.5 | tk 0.1.0

Hello everyone

It is required to change the Background Color -bg- of a Root Window -root- over time. It has been tested with the for and sleep statement, but it generates errors.

How to change the Background Color of the TK Object over time?
2° Can you achieve a Gradient Animation when moving from one Color to another?

Thank you very much already

Hi,

I don’t think it’s possible to do this (change the Background Color -bg- of a Root Window -root- over time), but it is possible to change the Background Color of a TK Object, which is an entirely different proposition. The reason being, that (so far as I know, from my experience) the only way to have said change take effect, once an object has been created and drawn, using whatever geometry manager you choose, is to .destroy() said object, then recreate it with a different bg attribute. For very obvious reasons, that can’t be done with the root window.

Maybe this function (from one of my apps) will demonstrate.

def verify():
    global publishedText
    rt = resultText.get('1.0', tk.END)
    rt = rt.strip()
    print('Result:',rt)
    pt = publishedText.get('1.0', tk.END)
    pt = pt.strip()
    if rt and pt != '\n':
        if rt == pt:
            publishedText.destroy()
            publishedText = tk.Text(publishedLF,
                                 width=63,
                                 height=2,
                                 bg='green',
                                 fg=textColor,
                                 font=(appFont)
                                 )
            publishedText.insert('1.0', pt)
            publishedText.place(relx=0.01, rely=0.01)
        else:
            publishedText.destroy()
            publishedText = tk.Text(publishedLF,
                                 width=63,
                                 height=2,
                                 bg='red',
                                 fg=textColor,
                                 font=(appFont)
                                 )
            publishedText.insert('1.0', pt)
            publishedText.place(relx=0.01, rely=0.01)

I’ve never tried to create a Gradient Animation and as such I can’t answer that part of your question.

edit to add…

Thinking some more about the Gradient Animation question, at least in theory, this could be done with a multiple create and destroy operation, controlled with a program loop, successively changing the bg attribute with each loop.

edit (again) to complete this train of thought…

Given that you’d need at least 15 f.p.s to get an acceptable animation, I don’t think that the above solution is practical, but it may be worth your time to try it, if you’ve nothing to lose.

That tk version must be incorrect. The current version from the Tcl/Tk website:

Tcl/Tk 8.6.12 Source

I found a package tk on www.pypi.org with that version number and it is about “Tensorkit”. Remove that, if that is what you meant.

Your Python comes with Tcl and Tk included. It is part of the Standard Library and you don’t have to install anything else to use it.

That is not going to work. A sleep puts the whole program to sleep, and tk will not be able to update the window. You are looking for after, which will generate an event after a specified time. Event handling is key to GUI programming, it may be good to find out something about that.

We are not looking over your shoulders, it is very hard to help with something as unspecific as “errors”. Copy the full traceback as pre-formatted text into your question to make it easier to help. Pre formatted text is preceded and followed by a line with three backticks (´). It may also be helpful to have some code, following the rules on http://www.sscce.org/.

1 Like

Try the following code in the Python repl:

>>> import tkinter as tk
>>> win = tk.Tk()

Wait until the screen appears. Then type:
>>> win.configure(bg="green")
And the background of the window turns green.

The configure method is available on (maybe almost) all widgets.

2 Likes

Excellent!! Thank you. I was not aware that the configure method could be used on-the-fly like that.

This is good to know.

+1

Thanks to @Mholscher I’ve updated my app thus:

def verify():
    global publishedText, rt, pt
    rt = resultText.get('1.0', tk.END)
    rt = rt.strip()
    print('Result:',rt) #debug line to remove
    pt = publishedText.get('1.0', tk.END)
    pt = pt.strip()
    if rt and pt != '\n':
        if rt == pt:
            publishedText.configure(bg="green")
        else:
            publishedText.configure(bg="red")

Given that this operation is way faster than what I had before, maybe this method is quick enough to create the Gradient Animation that @Skar is looking to do, using a control loop as I suggested.