How to make a button flashing?

Terry Reedy tjreedy at udel.edu
Wed Jun 13 13:16:26 EDT 2018


On 6/13/2018 7:21 AM, Peter Otten wrote:
> huey.y.jiang at gmail.com wrote:

>> I forgot the syntax of making a button flash, and failed to find an
>> example in the web. I use Tkinter, to create a button. Then made it
>> packed, and showed up. Now I am trying to make button b flashing, to make
>> it get better noticed. I checked the docs, and found a method flash() is
>> available, then I tried:
>>
>> b.flash()
>>
>> there was no error message popped out, but there was nothing happened
>> also. I wonder who can give me this help? Thanks!

> Quoting http://tcl.tk/man/tcl8.6/TkCmd/button.htm#M16
> 
> """
> pathName flash
>      Flash the button. This is accomplished by redisplaying the button
> several times, alternating between the configured activebackground and
> background colors. At the end of the flash the button is left in the same
> normal/active state as when the command was invoked. This command is ignored
> if the button's state is disabled.
> """
> 
> So to see something you have to ensure that the two background colors
> actually differ. Complete example:
> 
> import tkinter as tk
> root = tk.Tk()
> 
> button = tk.Button(root, text="Hello")
> button.pack()
> 
> button["activebackground"] = "red"
> button["background"] = "silver"
> 
> root.after(1000, button.flash)
> root.mainloop()

On Windows, this flashes red about 3 time in about .2 seconds and stops. 
  The following repeat the flashing every second, and shows how to stop 
an animation.

import tkinter as tk
root = tk.Tk()

def stop_flash():
     print('stop_flash')
     root.after_cancel(flasher)

button = tk.Button(root, text="Hello", command=stop_flash,
                    background='silver', activebackground='red')
button.pack()

def flash():
     global flasher
     button.flash()
     flasher = root.after(1000, flash)

flasher = root.after(1000, flash)
root.mainloop()



-- 
Terry Jan Reedy




More information about the Python-list mailing list