Page 1 of 1

Flashing Text in Tkinter

Posted: Fri Jan 04, 2019 6:49 pm
by Myles9
Hi,

I was wanting to have some flashing text in Tkinter. I didn't know where to start at all, so could someone help me out?

Thanks a lot!

Re: Flashing Text in Tkinter

Posted: Sat Jan 05, 2019 3:55 am
by Paeryn
When you want to flash the text (e.g. by changing its colour) have a function that swaps its colour and periodically calls itself using the after(msec_delay, function, args) method of the top level widget (root in my example).

Note that in the call to after(), function is the name of the function to call after the delay, don't put brackets after it like you would if you were actually calling the function as you don't want to call the function at that moment, if the function wants arguments when called just list them after the name, e.g.

Code: Select all

root.after(500, flashColour, object, 1 - colour_index)
says after 500 msecs (1/2 a second) call flashColour(object, 1 - colour_index)

A quick example, press the button to start the text flashing and again to stop it. This won't flash whilst the mouse is over the button due to how buttons work, but it is a quick example.

Code: Select all

import tkinter as Tk

flash_delay = 500  # msec between colour change
flash_colours = ('black', 'red') # Two colours to swap between

button_flashing = False

def flashColour(object, colour_index):
    global button_flashing

    if button_flashing:
        object.config(foreground = flash_colours[colour_index])
        root.after(flash_delay, flashColour, object, 1 - colour_index)
    else:
        object.config(foreground = flash_colours[0])

def buttonCallback(self):
    global button_flashing

    button_flashing = not button_flashing
    if button_flashing:
        self.config(text = 'Press to stop flashing')
        flashColour(self, 0)
    else:
        self.config(text = 'Press to start flashing',
                    foreground = flash_colours[0])

root = Tk.Tk()
my_button = Tk.Button(root, text = 'Press to start flashing',
                      foreground = flash_colours[0],
                      command = lambda:buttonCallback(my_button))
my_button.pack()

root.mainloop()

Re: Flashing Text in Tkinter

Posted: Sun Jan 06, 2019 5:48 pm
by Myles9
Is it possible to make a label flash/change colors every 500 ms? I'm trying to make something where some text "disappears" (by turning white), and then turns red 500 ms later, and so on.

Re: Flashing Text in Tkinter

Posted: Sun Jan 06, 2019 7:49 pm
by Paeryn
Yes, that's what the code I gave will do, the flash_colours tuple is the two colours to alternate between (the code is set up so the non-flashing state uses the first colour). Calling flashColour() with the first parameter being the Tkinter widget to flash (as long as it uses foreground as that is what the function changes) and the second as either 0 or 1 (which of the two colours to set first). As long as the global variable button_flashing the function will schedule to call itself after flash_delay msecs (which I set to be 1/2 a second) with the other colour.

You'll need to make modifications to fit it with your code, it is just an example of how to change a widget's configuration (e.g. its foreground colour) and how to get Tkinter to automatically call a function after a certain amount of time.

The way I did it is the last thing the flashing function does is to tell Tkinter to call itself after half a second with the other colour if button_flashing is true. Then all you have to do to start something flashing is to set button_flashing to True and call flashColour(), then to stop it you just set button_flashing to False.

I would do it slightly differently if I was going to use it myself (e.g. to not rely on globals, make it more reusable for multiple widgets etc.) but the principle would be the same.

Re: Flashing Text in Tkinter

Posted: Sun Jan 06, 2019 10:24 pm
by Myles9
OK, thanks! Got it to work by modifying your code a bit.
I put it down below just in case anyone wants it.

Code: Select all

import tkinter as Tk

flash_delay = 500  # msec between colour change
flash_colours = ('black', 'red') # Two colours to swap between

def flashColour(object, colour_index):
    object.config(foreground = flash_colours[colour_index])
    root.after(flash_delay, flashColour, object, 1 - colour_index)

root = Tk.Tk()
my_label = Tk.Label(root, text = 'I can flash!',
                      foreground = flash_colours[0])
my_label.pack()

flashColour(my_label, 0)

root.mainloop()
Edit: This actually flashes constantly, but you could just add the "button_flashing" variable back to regulate it.

Re: Flashing Text in Tkinter

Posted: Thu Feb 14, 2019 4:59 pm
by Chris1902
Thank you Paeryn & Myles9...

Really appreciate your contribution. You guys are awesome.

I'm working on this little project for my home monitoring. Kind of a security monitor where a display tells me that the garage door is open or the front door is unlocked. Can you share me a code wherein I got, say 2 gpio inputs (switches on front and garage doors ), where when pulled down it flashes a text on a label widget, say "front door open". If the 2 switches are pulled down at the same time the same label widget will flash "front door open" and "garage open" alternately.

I'm new to phyton and raspberry pi, I have a lot to learn. Your support would be really appreciated.

Thanks in advance.

Chris