Version 4 of A fading window

Updated 2014-10-13 19:17:12 by Superlinux

Arjen Markus (20 april 2013) Here is a little script to create a window that slowly fades and then vanishes. Some e-mail programs (Thunderbird) for instance use this type of window when a new e-mail arrives.

(Note: the fading window appears in the lower-right corner)

# fading.tcl --
#     Create a transient window that slowly fades away
#
toplevel .fading

wm overrideredirect .fading 1
wm geometry .fading -0-0
raise .fading

pack [label .fading.label -text "Slowly fade away ..."]

tkwait visibility .fading

proc fade {w degree} {
    wm attributes $w -alpha $degree
    if { $degree > 0 } {
        after 200 [list fade $w [expr {$degree-0.05}]]
    } else {
        destroy $w
    }
}

after 1000 {fade .fading 1.0}

Kevin Walzer: The fading alogrithm is useful for other things also, such as a Cocoa-style popover on Mac.


Superlinux - 2014-10-13 19:17:12

OK.. I used Arjen's code above and update it to look as much it can be like it is on Android Toast message. This feature is called on Android a Toast message. if you at the same run made ,for example like 20 messages one after the other, it will fade them all one by one very much like Android's. So here is the updated code. it's a procedure you can call it as much as you like and its message is centered on the screen.

Usage example:

#1st parameter is the message you want to display
#2nd parameter is the number of milliseconds to keep the message on.

tk_toastMessageBox "my toastmessage" 1000 

Here is the procedure. just copy it as is and add it to your application:

proc tk_toastMessageBox { message fade_time } {
        
        proc fade {w degree} {
                    wm attributes $w -alpha $degree
                    if { $degree > 0 } {
                        after 200 [list fade $w [expr {$degree-0.05}]]
                    } else {
                        destroy $w
                    }
                }
        
        #find the next toplevel window name available. 
        set counter 1
        while { [winfo exists ".fading_$counter" ]==1 } { incr counter } 
        
        set toplevel_window_name ".fading_$counter"
        toplevel $toplevel_window_name
        
        wm overrideredirect $toplevel_window_name 1
        
        #put the message toplevel window in the center of the screen
        set window_center_x_coords [ expr {([winfo screenwidth .]-[winfo width $toplevel_window_name])/2} ]
        set window_center_y_coords [ expr {([winfo screenheight .]-[winfo height $toplevel_window_name])/2} ]
#        set window_center [ join [ list $window_center_x_coords "x" $window_center_y_coords ] ]
#        set window_center [string map {" x " "x" } $window_center ]
        wm geometry $toplevel_window_name "+$window_center_x_coords+$window_center_y_coords"
         
        #raise $toplevel_window_name
        
        pack [label "$toplevel_window_name.label" -text  $message ]
        
        tkwait visibility  $toplevel_window_name
        
        
        set max_count $counter
        for { set count $max_count } { $count >0} { incr count -1 } {
                if {[winfo exists ".fading_$count" ]==1 } { 
                        raise ".fading_$count" 
                        
                }
                
        }
        after [ expr $fade_time*$max_count ] [ list fade $toplevel_window_name 1.0 ]
}