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